Skip to content

Commit a507f23

Browse files
committed
Introduce a function verify_current_test_outcome.
This allows a test to check whether the current test has passed or failed based on non-fatal assertions such as `expect_that!`. Its naming follows the pattern of `verify_that!` since it uses the same idiom of returning a `googletest::Result`.
1 parent 5351320 commit a507f23

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-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: 32 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,37 @@ 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 should_fail_and_not_execute_last_assertion() -> Result<()> {
106+
/// # googletest::internal::test_outcome::TestOutcome::init_current_test_outcome();
107+
/// let value = 1;
108+
/// expect_that!(value, eq(2)); // Fails but does not abort the test.
109+
/// verify_current_test_outcome()?; // Aborts the test due to the previous failure.
110+
/// verify_that!(value, eq(1)) // Does not execute.
111+
/// }
112+
/// # verify_that!(should_fail_and_not_execute_last_assertion(), err(displays_as(contains_substring("Test failed")))).unwrap();
113+
/// ```
114+
pub fn verify_current_test_outcome() -> Result<()> {
115+
TestOutcome::get_current_test_outcome()
116+
}
117+
86118
/// Adds to `Result` support for GoogleTest Rust functionality.
87119
pub trait GoogleTestSupport {
88120
/// If `self` is a `Result::Err`, writes to `stdout` a failure report

0 commit comments

Comments
 (0)