Google's c ++ unit test framework gtest

Source: Internet
Author: User

This open-source Project hosted by Google, Http://code.google.com/p/googletest/ (gtest) It has been used in many other open-source projects of Google. For example, the advantage of chromium gtest is that the writing case is simple and cross-platform. It has certain scalability. Use the test macro to add our Code You can generate a case, for example, test (factorialtest, zero) {expect_eq (1, factorial (0);} Here factorialtest is the case name, zero is the test name expect_eq. This macro indicates that the expected values are equal. Here we expect the function factorial (INT) to be written. If the parameter is 0, 1 is returned. How can we execute this test case? Write the following code in the main function: int _ tmain (INT argc, _ tchar * argv []) {testing: initgoogletest (& argc, argv ); return run_all_tests ();} testing: initgoogletest (& argc, argv); this sentence is not necessary, but it is only necessary to pass the execution parameters to the test framework to run the test. Program , Gtest will show the detailed test process. Obviously, an expect_eq macro is not enough for us to use. gtest provides us with more judgment macros: the bool type has expect_true, expect_false numeric types include expect_eq, expect_ne, expect_lt, expect_le, expect_gt, and expect_ge strings with expect_streq, expect_strne, distinct, and distinct. If an exact CT _ * fails, the case will continue, if you want to stop, gtest provides a corresponding assert _ * for each of your CT _ * cases to forcibly return a successful result. If you want to call succeed (), otherwise, you only need to call fail (). If you want to add your own output during the test, you only need to add <"your output" to the macro of gtest. In addition to the simple check above, exception determination is also supported, because sometimes we return errors by exceptions. By inheriting testing: Environment and implementing setup and teardown, you can extend this test framework. In addition, there is a more complex test_f waiting for you. complexity means that the implementation is complicated, but the use is not complicated. Google provides us with a very good wheel. If you are interested, you can check the code of SVN Co gtest, which contains sample and Wik.

Other C ++ unit test tools include:

Cppunit (nunit for C #)

A tool that can make create C ++ test cases using nunit.

Related Article

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.