How to jump out of the current test case in gtest

Source: Internet
Author: User
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 ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.