Add ability to throw from ASSERT

while not losing benefits of EXPECT, and not killing the whole test,
as with --gtest_throw_on_failure.

183822976
This commit is contained in:
Alexey Sokolov
2018-02-03 23:36:19 +00:00
parent ea31cb15f0
commit 092d088533
5 changed files with 171 additions and 8 deletions

View File

@@ -872,13 +872,33 @@ TEST(FooTest, Bar) {
}
```
Since we don't use exceptions, it is technically impossible to
implement the intended behavior here. To alleviate this, Google Test
provides two solutions. You could use either the
`(ASSERT|EXPECT)_NO_FATAL_FAILURE` assertions or the
`HasFatalFailure()` function. They are described in the following two
To alleviate this, gUnit provides three different solutions. You could use
either exceptions, the `(ASSERT|EXPECT)_NO_FATAL_FAILURE` assertions or the
`HasFatalFailure()` function. They are described in the following two
subsections.
#### Asserting on Subroutines with an exception
The following code can turn ASSERT-failure into an exception:
```c++
class ThrowListener : public testing::EmptyTestEventListener {
void OnTestPartResult(const testing::TestPartResult& result) override {
if (result.type() == testing::TestPartResult::kFatalFailure) {
throw testing::AssertionException(result);
}
}
};
int main(int argc, char** argv) {
...
testing::UnitTest::GetInstance()->listeners().Append(new ThrowListener);
return RUN_ALL_TESTS();
}
```
This listener should be added after other listeners if you have any, otherwise
they won't see failed `OnTestPartResult`.
### Asserting on Subroutines ###
As shown above, if your test calls a subroutine that has an `ASSERT_*`