Boost Test Library (zz)

Source: Internet
Author: User

Address: http://www.cppblog.com/shuiyuan2004/archive/2008/03/29/45677.html

Boost Test


The Test Library contains the following components:
Execution MonitorAn exception and error detection and reporting mechanism is basically used for program and test program. Execution monitor calls user-provided functions and reports all caught runtime exceptions, it is only called by other boost Test Library components internally. Of course, it can also be used for some production environment to control which causesProgramCall of the crashed function;

Program Execution MonitorA simple helper facility is used to monitor the running of a program. Program Execution monitor provides the main () function and execution monitor program execution, which can be used to generate consistent error reports for production environment, controls programs running in the test environment and directly uses test execution monitor;

Test ToolsA toolbox for testing. test tools is used to test programs running under the control of test execution monitor or unit test framework;

Test execution MonitorLet a test program run in the monitored environment. test execution monitor provides main () to control the running of the tested program and allows test tools to implement the test logic, it is used in test environment. If you want to control the running of production code, use program execution monitor;

Unit Test FrameworkIt is used to simplify the compilation and organization of the test cases framework. It supports Test Cases Written by simple functions or member functions and organizes them into a tree of Test suites, this framework uses test tools to implement test cases and provides a mechanism to manage log report level and result report level;

Minimal testing facilitySimple facility of functions provided by boost test in the initial version, providing the same mechanism as test execution monitor, but defining some test tools that simply provide similar functions of test tools, it does not need to be linked with any external component. It is suitable for simple and fast testing and is used in test environment.
(Http://yaekees.spaces.live.com/blog/cns! 1955ee8c6707277a! 146. Entry)

Execution Monitor of boost Test

Use the execution monitor trilogy:
1. # include <boost/test/execution_monitor.hpp>
2. Make an instance of Boost: execution_monitor
3. Optionally register custom exception translators for exception classes you want special processing

Call execution_monitor: Execute (function_to_monitor, catch_system_exception, timeout) to run the monitored function. If the call is successful, an integer value is returned. If the following happens
1. uncaught C ++ exception
2. hardware or software signal, trap, or other exception
3. Timeout reached
4. debug assert event occurred (under Microsoft Visual C ++ or compatible compiler)
The method execution_monitor: Execute (...) throws the boost: execution_exception
If you want the program error message to be converted to the error message of execution_exception, the following three types of exceptions are thrown: C string, STD: String, any exception class in STD: exception hierarchy.

The best way to terminate monitored function and prevent execution monitor from reporting any error is to throw boost: execution_aborted. If you do not like "unknown exception caught" message and prefer to use a custom exception, you can register the translator function with execution monitor as any exception types, as shown in
Ex_mon.register_exception_translator <my_exception1> (& translate_my_exception1 );
My_exception1 is the exception type, and translate_my_exception1 is the exception handling function.
Class execution_monitor {
Public:
Virtual ~ Execution_monitor ();

Template <typename exception, typename exceptiontranslator>
Void register_exception_translator (exceptiontranslator const & TR, boost: Type <exception> * = 0 );

Int execute (unit_test: callback0 <int> const & F, bool catch_system_errors = true, int timeout = 0 );
}; // Exception Monitor

Execution monitor uses boost: execution_exception to report Captured problems. Its biggest feature is that it does not allocate any memory and is therefore used in memory-scarce environments.
Class execution_exception {
Public:
Execution_exception (error_code EC, const_string what_msg );
Enum error_code {
Cpp_exception_error, // See note (1) below
User_error, // user reported nonfatal Error
System_error, // See note (2) below
Timeout_error, // only detectable on certain platforms
User_fatal_error, // user reported fatal error
System_fatal_error // See note (2) below
};
Error_code code () const; // use this method to get an error code for the exception
Const_string what () const; // use this method to get an error message for the exception
};
Note 1: uncaught C ++ exceptions is treated as an error. If the application captures a C ++ exception, the exception will not go to boost: execution_monitor;
NOTE 2: these errors include UNIX signals and Windows structured exceptions, which are often triggered by hardware traps.

Execution_monitor can be dynamically connected. The main function is not provided in the library. Since there is only one libs/test/execution_monitor.cpp file, you can copy the file directly.
(Http://yaekees.spaces.live.com/blog/cns! 1955ee8c6707277a! 148. Entry)

Program Execution Monitor of boost Test

C ++ program can report user-detected errors through return value and throwing an exception, while system-detected errors of dereferencing an Invalid Pointer reports in other ways.

The program execution Monitor of boost Test Library reduces the complexity of error detection and reporting, and provides the main () function in Monitored Environment to call the cpp_main () function provided by the user, the main () function detects and reports the occurrence of multiple errors in a consistent manner, and converts it into a consistent return code and returns it to the host enviroment.
Boost_test_catch_system_errors allows execution monitor to capture system errors. The default value is "yes ".
Boost_prg_mon_confirm specifies whether to allow user interaction to confirm that the program runs correctly. The default value is "yes ".

Program Execution monitor uses execution monitor to monitor the running of the User-provided cpp_main () function. Although program execution monitor provides the main () function in libs/test/src/cpp_main.cpp, to ensure the link is correct, you must provide a cpp_main () interface with the same interface as the main () function () function. If cpp_main () throws an exception or returns a non-0 value, the program execution monitor considers the program to have an error.
The program execution monitor provides detailed and brief error reports on the cout and cerr streams. The main () function provided by program execution monitor returns the following values:
1. Boost: exit_success-no errors
2. Boost: exit_failure-non-zero and non-Boost: exit_success return code from cpp_main ().
3. Boost: exit_exception_failure-cpp_main () throw an exception.
(Http://yaekees.spaces.live.com/blog/cns! 1955ee8c6707277a! 149. Entry)

Test Tools of boost Test

To make it easier to use, test tools provides a series of macros, which are divided into three levels, with different effects:
Warn does not add reference count, continue to execute the program
Check to increase the application count and continue executing the program
Require increases application count and interrupts program running

Use check level tools to implement assertions. Use warn level tools to verify unimportant but correct aspects, such as performance, portability, and usefulness, if the assertions fails, you should not allow the program to continue running. Use require level tools.

Test Tools provides two. HPP files: boost/test/test_tools.hpp and boost/test/floating_point_comparison.hpp. One. cpp file is libs/test/test_tools.cpp.
Boost_warn (P)
Boost_check (P)
Boost_require (P)
Boost_warn_message (p, m)
Boost_check_message (p, m)
Boost_require_message (p, m)
Boost_error (m) boost_fail (m)
Boost_message (m)
Boost_checkpoint (m)
Boost_warn_throw (S, E)
Boost_check_throw (S, E)
Boost_require_throw (S, E)
Boost_warn_exception (S, E, P)
Boost_check_exception (S, E, P)
Boost_require_exception (S, E, P)
Boost_ignore_check (E)
Boost_warn_no_throw (s)
Boost_check_no_throw (s)
Boost_require_no_throw (s)
Boost_warn_close (L, R, T)
Boost_check_close (L, R, T)
Boost_require_close (L, R, T)
Boost_warn_small (FPV, T)
Boost_check_small (FPV, T)
Boost_require_small (FPV, T)
Boost_warn_predicate (p, argS)
Boost_check_predicate (p, argS)
Boost_require_predicate (p, argS)
Boost_warn_equal_collections (l_begin, l_end, r_begin, r_end)
Boost_check_equal_collections (l_begin, l_end, r_begin, r_end)
Boost_require_equal_collections (l_begin, l_end, r_begin, r_end)
Boost_warn_bitwise_equal (L, R)
Boost_check_bitwise_equal (L, R)
Boost_require_bitwise_equal (L, R)
Boost_is_defined (symb)
Boost_bitwise_equal (L, R)
(Http://yaekees.spaces.live.com/blog/cns! 1955ee8c6707277a! 145. Entry)

Test execution Monitor of boost Test

Test execution monitor simplifies the tedious test work by combining the features of test tools and execution monitor. It provides the main () function to call the user-provided test_main () function, you can use test tools for complex verification.
 
Test execution monitor is designed to test simple programs or dig a problem from an existing production code. Program Execution monitor is more suitable for monitoring production (non-test) Programs (because it does not affect program performance), while unit test framework is more suitable for complex test programs, because unit test framework can
1. You can divide test into multiple test cases, which will generate pass/fail statistics for each test case;
2. If a test case fails, other test cases will not be affected;
3. You can run a specific test case by specifying the name.
4. Separated test cases run more clearly to find the purpose of a specific test module
5. You can set more options.
(Http://yaekees.spaces.live.com/blog/cns! 1955ee8c6707277a! 150. Entry)

Boost test unit test framework

Regression testing only focuses on whether errors occur when the program is running, while unit test needs to output the error information as detailed as possible. Unit test framework uses test tools to simplify the compilation of test cases and organize them into hierarchical test suites. It provides the main () function to initialize the framework, set parameters through command line parameters or environment variables, and use init_unit_test_suite (argc, argv), and then run the user's test suite. Framework tracks all test tools assertions of passed/failed, and obtains the Test Progress by using the number of test cases (part and total), and provides results in multiple forms. The unit test framework can be used for simple tests and complex and important tests. It is not suitable for production code and accelerates compilation at the cost of runtime efficiency.

This function is used to create and initialize the test_suite instance at the top level. For example, if test_suite fails to be created, the function returns a null pointer, ends the test, and returns a boost: exit_test_failure. The framework passes specific command line parameters during the test run, and rejects parameters specified by other frameworks. The framework is responsible for the lifecycle of test_suite and will be destroyed when test is terminated.

If test cases will lose a custom exception, you can register a specific translator like execution monitor. The prototype of the registered function is as follows:
Template <typename exception, typename exceptiontranslator>
Void boost: unit_test: register_exception_translator (predictiontranslator const & TR, boost: Type <exception> * D = 0)

Once the test is complete, the Framework reports the results and returns return code. The returned values integrated in the unit test framework are as follows:
Boost: exit_success returned if no errors occurred during test or success result code was explicitly requested with the no result code framework Parameter
Boost: exit_test_failure returned if nonfatal errors detected and no uncaught exceptions thrown or the framework fails to initialize the test suite
Boost: exit_exception_failure returned if fatal errors detected or uncaught exceptions thrown

Unit_test_example3.cpp failed on vc7.1 + stlport 4.62 (not passed for multiple types of inheritance)

Simple usage:
1. First define # define boost_auto_test_main
2. Include # include <boost/test/auto_unit_test.hpp>
3. Create a test_suite
Boost_auto_test_case (test)
{
Boost_check (true );
}
Then the link libboost_test_exec_monitor-vc71-mt-sp-1_33.lib is ready.

The other method includes the following:
# Include <boost/test/unit_test.hpp>
# Include <boost/test/unit_test_monitor.hpp>
Using namespace boost: unit_test;
Then declare test_suite * init_unit_test_suite (INT/* argc */, char */* argv */[]) {}
A typical usage is
Test_suite * init_unit_test_suite (INT/* argc */, char */* argv */[]) {
Test_suite * test = boost_test_suite ("custom_exception_test ");
Unit_test_monitor.register_exception_translator <my_exception1> (& my_exceptionexceptiontranslator );
Unit_test_monitor.register_exception_translator <my_exception2> (& my_exception2_translator );
Test-> Add (boost_test_case (& throw_my_exception1 ));
Test-> Add (boost_test_case (& throw_my_exception2 ));
Return test;
}
(Http://yaekees.spaces.live.com/blog/cns! 1955ee8c6707277a! 151. Entry)

Boost Minimal testing facility

It is only applicable to test environment and does not need to link any external components. You only need to provide the following functions:
Int test_main (INT argc, char * argv. Minimal testing facility provides boost_check (predicate), boost_require (predicate), boost_error (Message), boost_fail (Message ). In addition to the four macro statements, an error can be reported by throwing an exception and returning a return value. The following is an example provided by boost.
# Include <boost/test/minimal. HPP>
Int add (int I, Int J) {return I + J ;}
Int test_main (INT, char * []) // note the name!
{
// Six ways to detect and report the same error:
Boost_check (add (2, 2) = 4); // #1 continues on Error
Boost_require (add (2, 2) = 4); // #2 throws on Error
If (add (2, 2 )! = 4)
Boost_error ("ouch..."); // #3 continues on Error
If (add (2, 2 )! = 4)
Boost_fail ("ouch..."); // #4 throws on Error
If (add (2, 2 )! = 4) throw "Oops..."; // #5 throws on Error
Return add (2, 2) = 4? 0: 1; // #6 Returns Error Code
}
Boost_check if the expression failsSource code File Name, Code The row number, and the error count will be added. Once the program terminates the error count, it will be displayed in STD: cout;
Boost_require is similar to boost_check, but an exception caught by minimal testing facility is thrown.
(Http://yaekees.spaces.live.com/blog/cns! 1955ee8c6707277a! 147. Entry)

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.