In the previous example, we mentioned that assert _ * series assertions are only returned in the current function, rather than exiting the current test case, because the assert _ * series are implemented through return (therefore, the assert _ * series cannot appear in functions whose return value is not void ). To exit the current test case, the simplest way is to throw an exception and let gtest capture the exception. Example:
Void Func ( Int A, Int B)
{
Throw " B = 0 " ;
Expect_eq ( 0 , A \ B );
Printf ( " End of func " );
}
Test (footest, demo1)
{
Func (5,0);
Printf ("End call func (5, 0)");
}
Int_ Tmain (IntArgc, _ tchar*Argv [])
{
Testing: gtest_flag (catch_exceptions)= 1;
Testing: initgoogletest (&Argc, argv );
ReturnRun_all_tests ();
}
(Neither of the above two printf functions will be executed .)
To exit the current test case, you only need two steps:
1. Set the catch_exception flag, which can be used before the main function or your test case.
2. When jumping out of the test case, you only need to throw any exception through throw.
Why can I skip the current test case through this method? refer to the tutorial gtest-to analyze gtest in depth.
Note that:If test_f macro is used, teardown () is executed after jumping out of the current test case. Therefore, you do not have to be careful when releasing resources in teardown.
I think a good test case should be in your test function, such as the test macro,Clearly express the objects you want to test and the expected test results. Therefore, generally, expect CT _ * and assert _ * should appear in the test function as much as possible, rather than in another function called in the test function or in a very multi-layer function. (For example, expect_eq in the above func function ).