Boost Test Learning Summary (c + +)

Source: Internet
Author: User

1. Common C + + unit Testing Framework

Test-driven Development (TDD) is already a very popular development approach, providing a very good unit testing framework in both Java and. NET, and recently researching unit tests under C + +, and discovering that there are many choices in C + +:

CPPUnit: One of the famous Xunit products, familiar with JUnit, NUnit developers can quickly get started.

Cxxtest: Preprocessing is required, Perl or Python is installed.

Boost Test: Powerful, provides both automatic registration and manual registration, and more importantly from the tempered boost library.

Google Test:google, an open source test framework last year, is said to have used the framework for unit testing in thousands of its internal projects.

2.Boost Test Start

Let's take a look at a simple boost test sample (from the Boost documentation):

Test Preliminary

1#define Boost_test_module Example
2
3#include <boost/test/unit_test.hpp>
4
5
6
7int Add (int i, int j);
8
9

11boost_auto_test_suite (minimal_test)
[
]
15boost_auto_test_case (my_test)

17{

Boost_check (Add (2, 2) = 5);

Boost_require (Add (2, 2) = 4);

2 3 if (Add (2, 2)!= 4)
Boost_error
("oops!");

if (Add (2, 2)!= 4)

Boost_fail ("oops!");

if (Add (2, 2)!= 4)
Throw
"oops!";
Boost_check_message
(Add (2, 2) = = 4, add (..) Result: "<< Add (2, 2));

Boost_check_equal (Add (2, 2), 4),
"
"
"
"
"
" 43boost_auto_test_suite_end ()

First, you need to define #define Boost_test_module example or #define Boost_auto_test_main, otherwise the test module initialization function needs to be implemented manually; then the macro "Boost_auto_test_ The Suite (minimal_test) will create a test suite named Minimal_test and add it to the test module. The macro boost_auto_test_case (my_test) creates a test case named "My_test" and adds it to the test suite minimal_test.

One common mistake about test modules is to define different "#define Boost_test_module example" in different test files, which will eventually result in "multiple definition of" init_unit_test_suite ( int, char**) ' "error because only one definition of a test module is allowed in a test program. A master test suite exists in the test module, and any test cases that are not explicitly included in the test suite will be included in the Master test suite.

Here is a specific testing process that shows seven different ways to test the function add in a program:

Boost_check: This method saves the errors that are checked and is automatically displayed by the test framework at the end of the test;

Boost_require: The same check error, unlike Boost_check, is that an exception is thrown if an error occurs, so subsequent tests will not continue;

Boost_error: Can be used to describe the error independently, the test framework is automatically displayed at the end of the test;

Boost_fail: The same can be used to describe the error independently, the call will throw an exception;

Throw an exception: The test framework catches the exception and prints out the error message;

Boost_check_message: Similar to Boost_check, you can add error description information to the second parameter;

Boost_check_equal: Similar to Boost_checkl, used to check whether two parameters are equal.

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.