Skip to content

Commit e45d943

Browse files
Merge pull request #270 from google:add-verify-current-test-outcome
PiperOrigin-RevId: 549947272
2 parents 5351320 + 978f6d1 commit e45d943

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

googletest/src/internal/test_outcome.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,20 @@ impl TestOutcome {
8484
})
8585
}
8686

87+
/// Returns a `Result` corresponding to the outcome of the currently running
88+
/// test.
89+
pub(crate) fn get_current_test_outcome() -> Result<(), TestAssertionFailure> {
90+
TestOutcome::with_current_test_outcome(|mut outcome| {
91+
let outcome = outcome
92+
.as_mut()
93+
.expect("No test context found. This indicates a bug in GoogleTest.");
94+
match outcome {
95+
TestOutcome::Success => Ok(()),
96+
TestOutcome::Failure => Err(TestAssertionFailure::create("Test failed".into())),
97+
}
98+
})
99+
}
100+
87101
/// Records that the currently running test has failed.
88102
fn fail_current_test() {
89103
TestOutcome::with_current_test_outcome(|mut outcome| {

googletest/src/lib.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ pub mod matchers;
4343
pub mod prelude {
4444
pub use super::matcher::Matcher;
4545
pub use super::matchers::*;
46+
pub use super::verify_current_test_outcome;
4647
pub use super::GoogleTestSupport;
4748
pub use super::IntoTestResult;
4849
pub use super::Result;
@@ -83,6 +84,39 @@ use internal::test_outcome::{TestAssertionFailure, TestOutcome};
8384
/// failed but allow it to continue running, are not encoded in this type.
8485
pub type Result<T> = std::result::Result<T, TestAssertionFailure>;
8586

87+
/// Returns a [`Result`] corresponding to the outcome of the currently running
88+
/// test.
89+
///
90+
/// This returns `Result::Err` precisely if the current test has recorded at
91+
/// least one test assertion failure via [`expect_that!`][crate::expect_that],
92+
/// [`expect_pred!`][crate::expect_pred], or
93+
/// [`GoogleTestSupport::and_log_failure`]. It can be used in concert with the
94+
/// `?` operator to continue execution of the test conditionally on there not
95+
/// having been any failure yet.
96+
///
97+
/// This requires the use of the [`#[googletest::test]`][crate::test] attribute
98+
/// macro.
99+
///
100+
/// ```
101+
/// # use googletest::prelude::*;
102+
/// # /* Make sure this also compiles as a doctest.
103+
/// #[googletest::test]
104+
/// # */
105+
/// # fn foo() -> u32 { 1 }
106+
/// # fn bar() -> u32 { 2 }
107+
/// fn should_fail_and_not_execute_last_assertion() -> Result<()> {
108+
/// # googletest::internal::test_outcome::TestOutcome::init_current_test_outcome();
109+
/// expect_that!(foo(), eq(2)); // May fail, but will not abort the test.
110+
/// expect_that!(bar(), gt(1)); // May fail, but will not abort the test.
111+
/// verify_current_test_outcome()?; // Aborts the test if one of the previous assertions failed.
112+
/// verify_that!(foo(), gt(0)) // Does not execute if the line above aborts.
113+
/// }
114+
/// # verify_that!(should_fail_and_not_execute_last_assertion(), err(displays_as(contains_substring("Test failed")))).unwrap();
115+
/// ```
116+
pub fn verify_current_test_outcome() -> Result<()> {
117+
TestOutcome::get_current_test_outcome()
118+
}
119+
86120
/// Adds to `Result` support for GoogleTest Rust functionality.
87121
pub trait GoogleTestSupport {
88122
/// If `self` is a `Result::Err`, writes to `stdout` a failure report

0 commit comments

Comments
 (0)