UnitTest ++ usage

Source: Internet
Author: User

UnitTest ++ is a very small and convenient unit test tool used in the C ++ project. It provides functionality similar to CppUnit, But It minimizes the workload of case writing and makes it easier to use. All of these benefits from its clever conception of the framework, for details, refer to the source code. This tool does not support VC6 itself. I have improved it. Below is a simple comparison of CppUnit and UnitTest ++: 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = "../attachment/200909/200909101252594756573 .jpg" border =" 0 "alt =" "/>
1. Usage1.1. Prepare the environment 1.1.1. compile and generate the UnitTest ++. lib file.1.1.2. Set an environment variable UNITTEST_CPP_PATH.650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) "style =" border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px "height =" 351 "alt =" clip_image004 "src =" http://www.bkjia.com/uploads/allimg/131228/1S9322c4-0.jpg "313" = "" border = "0"/>1.1.3. For example, copy all the header files of UnitTest ++.650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) "style =" border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px "height =" 395 "alt =" clip_image006 "src =" http://www.bkjia.com/uploads/allimg/131228/1S9322340-1.jpg "229" = "" border = "0"/>1.1.4. Copy the compiled. lib file.650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) "style =" border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px "height =" 94 "alt =" clip_image008 "src =" http://www.bkjia.com/uploads/allimg/131228/1S9323Q4-2.jpg "372" = "" border = "0"/>1.2. engineering settingsThe following settings are required for the project to be tested.1.2.1. Add the header file search path:650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) "style =" border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px "height =" 289 "alt =" clip_image010 "src =" http://www.bkjia.com/uploads/allimg/131228/1S9324Y3-3.jpg "432" = "" border = "0"/>1.2.2. Add the library file search path:650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) "style =" border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px "height =" 289 "alt =" clip_image012 "src =" http://www.bkjia.com/uploads/allimg/131228/1S9323927-4.jpg "431" = "" border = "0"/>1.2.3. Load the lib file in the file where the main () function is located. Call UnitTest: RunAllTests () in the main () function. All test cases are executed.. 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) "style =" border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px "height =" 184 "alt =" clip_image014 "src =" http://www.bkjia.com/uploads/allimg/131228/1S932H12-5.jpg "427" = "" border = "0"/>1.3. Use Case writingYou can create a cpp file for each module to write test cases.1.3.1. Simple Test Cases650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = \ '# \' "/editer/InBlock.gif" align = "top"/> // test. cpp
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = \ '# \' "/editer/InBlock.gif" align = "top"/>#include <UnitTest ++. h>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = \ '# \' "/editer/InBlock.gif" align = "top"/> TEST (TestCaseName)
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = \ '# \' "/editer/InBlock.gif" align = "top"/> {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = \ '# \' "/editer/InBlock.gif" align = "top"/> int ret = TestFunction ();
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = \' # \ '"/editer/InBlock.gif" align = "top"/> CHECK (ret = SUCCESS );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = \ '# \' "/editer/InBlock.gif" align = "top"/>}
Note: 1. Each TEST case starts with TEST, followed by the case name in parentheses, and repeated tests are not allowed. 2. Generally, use the CHECK macro to CHECK the result. For more inspection methods, refer to the source code or the documentation provided by UnitTest ++. The following lists some commonly used macros: CHECK_EQUAL (expected, actual) Check for equal CHECK_CLOSE (expected, actual, tolerance) check whether the values are equal within the tolerable range of errors (mostly used for equal floating point numbers) CHECK_ARRAY_EQUAL (expected, actual, count) check whether the array is equal. The first two parameters are array names, count is the number of elements. CHECK_ARRAY_CLOSE (expected, actual, count, tolerance) refer to the preceding two descriptions. CHECK_ARRAY2D_CLOSE (expected, actual, rows, columns, tolerance) two-dimensional array. CHECK_THROW (expression, ExpectedExceptionType) checks whether an exception of the specified type is thrown. CHECK_ASSERT (expression) checks whether UnitTest: AssertException type exceptions are thrown. 3. Each test case is automatically added to the set of test cases to be run.1.3.2. test the use of SUITSUIT can be used if you want to place a set of test cases in a group. 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = \ '# \' "/editer/InBlock.gif" align = "top"/> SUITE (YourSuiteName)
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = \ '# \' "/editer/InBlock.gif" align = "top"/> {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = \ '# \' "/editer/InBlock.gif" align = "top"/> TEST (YourTestName)
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = \ '# \' "/editer/InBlock.gif" align = "top"/> {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = \ '# \' "/editer/InBlock.gif" align = "top"/>}
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = \ '# \' "/editer/InBlock.gif" align = "top"/> TEST (YourOtherTestName)
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = \ '# \' "/editer/InBlock.gif" align = "top"/> {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = \ '# \' "/editer/InBlock.gif" align = "top"/>}
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = \ '# \' "/editer/InBlock.gif" align = "top"/>}
1.3.3. Use of FIXTUREIf you want to execute a part of the code (setUP) before executing a set of use cases, and then execute a piece of code (tearDown) after the test is completed, or you want to share data in multiple use cases, you can use FIXTURE. 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = \ '# \' "/editer/InBlock.gif" align = "top"/> struct SomeFixture
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = \ '# \' "/editer/InBlock.gif" align = "top"/> {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = \ '# \' "/editer/InBlock.gif" align = "top"/> SomeFixture () {/* some setup */}
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = \ '# \' "/editer/InBlock.gif" align = "top"/> ~ SomeFixture () {/* some teardown */}
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = \ '# \' "/editer/InBlock.gif" align = "top"/> int testData;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = \ '# \' "/editer/InBlock.gif" align = "top"/> };
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = \ '# \' "/editer/InBlock.gif" align = "top"/> TEST_FIXTURE (SomeFixture, YourTestName)
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = \ '# \' "/editer/InBlock.gif" align = "top"/> {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = \ '# \' "/editer/InBlock.gif" align = "top"/> int temp = testData;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = \ '# \' "/editer/InBlock.gif" align = "top"/>}
Note: 1. To use FIXTURE, you must first define a structure SomeFixture. 2. Place the code to be run in setUp into the SomeFixture constructor. 3. Place the code to be run in tearDown into the destructor of SomeFixture. 4. Members in SomeFixture can be used to test data sharing. 5. Each use case is defined using TEST_FIXTURE. Similar to the TEST macro, the defined use case is automatically added to the set of use cases to be run.1.3.4. Restrict the execution time of some code in the use caseYou can limit that some of the Code in the use case can only be run within the specified time. If it cannot be run, use case fail ). 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = \ '# \' "/editer/InBlock.gif" align = "top"/> TEST (YourTimedTest)
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = \ '# \' "/editer/InBlock.gif" align = "top"/> {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = \ '# \' "/editer/InBlock.gif" align = "top"/> // Lengthy setup...
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = \ '# \' "/editer/InBlock.gif" align = "top"/> {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = \' # \ '"/editer/InBlock.gif" align = "top"/> UNITTEST_TIME_CONSTRAINT (50 );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = \ '# \' "/editer/InBlock.gif" align = "top"/> // Do time-critical stuff
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = \ '# \' "/editer/InBlock.gif" align = "top"/>}
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = \ '# \' "/editer/InBlock.gif" align = "top"/> // Lengthy teardown...
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = \ '# \' "/editer/InBlock.gif" align = "top"/>}
If the code in the Do time-critical stuff section is not completed within 50 ms, use case fail.1.4. Advanced usage1.4.1. You can customize functions by modifying UnitTest ++ source code.650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = \ '# \' "/editer/InBlock.gif" align = "top"/> int RunAllTests ()
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = \ '# \' "/editer/InBlock.gif" align = "top"/> {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = \ '# \' "/editer/InBlock.gif" align = "top"/> TestReporterStdout reporter;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) "src = \ '# \'"/editer/InBlock.gif "align =" top "/> return RunAllTests (reporter, Test: GetTestList (), 0 );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = \ '# \' "/editer/InBlock.gif" align = "top"/>}
1. In the UnitTest ++ source code, modify the previous RunAllTests (reporter, Test: GetTestList (), 0) last parameter 0. You can set global time constraints, that is to say, the execution time of each use case must be less than this time constraint. Otherwise, use case fail. 2. You can change the reporter type to change the test result output mode. For example, XmlTestReporter is used to output the result to an xml file.1.4.2. More advanced usageLearn UnitTest ++ source code and UnitTest ++ test code. The test code of UnitTest ++ is well written. You can learn about the functions of UnitTest ++ and the writing methods of test cases.

This article is from the "Jelly software technology blog" blog, please be sure to keep this source http://sinojelly.blog.51cto.com/479153/200792

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.