One of the unit test job guidance series

xiaoxiao2021-03-06  110

This is what I have previously served as the project manager, and the work instructions on unit tests are prepared, and the unit testing and environmental configuration is made for a variety of development environment narratives. Now it is sorted out. It should be helpful to everyone.

This is the first part, mainly for C and C items (including Windows Environment and Linux Environments), and the lower part will be for Java and J2EE projects.

1. Purpose

In order to reduce the number of errors in the code, reduce the time and energy of debugging, improve software quality, reduce development and maintenance time and cost.

2. Scope of application

Suitable for all products of C and C .

3. Applicable content

3.1 C standard

3.1.1 Test Environment Use Visual C , Windows Window Applications

3.1.1.1 For the Former Project: After using the CPPUnit1.6.2, after decompression, the path is x: //cppunit-1.6.2;

Configuring a test framework in an engineering document: Add to the path of the executive header x: //cppunit-1.6.2/include, add the path of the import library file x: //cppunit-1.6.2/lib;

Configure the Debug (Test) version environment:

Join the static test box module Testrunnercd.lib (Select dialog box for running test case) and cppunitcd.lib (test framework);

Add Test Add-Ins, the library is x: //cppunit-1.6.2/lib/testrunnerdsplugind.dll;

Enable RTTI in Project Settings / C / C Language;

3.1.1.2 Establish test cases:

1. Take the Test "name test unit file name, such as the" CMAbstring "class name, such as the" cmabstring "class, named MabString.cpp, the test unit file is named TestMabString.cpp;

2, join the test frame header file and the unit header to test, take TestMabString as an example:

Header file: TestmabString.h

#ifndef cpp_unit_testnode_h # define cpp_unit_testnode_h // Contains the header file #include #include // contains the header file #include "mabstring.h" with the test unit. // testing framework derived test class class TestMabString: public CppUnit :: TestCase {// test defined list, which will appear in the dialog box choose to run the test case CPPUNIT_TEST_SUITE (TestMabString); CPPUNIT_TEST (findByName); CPPUNIT_TEST_SUITE_END ( ); Protected: // cmabstring m_mabstr; public: // uses an example initialization, can be used as a pile function void setup (); // use examples void Teardown (); protected: // test case void findbyname (void);}; endif class file: testmabstring.cpp # include "TestMabString.h" #include "iostream.h" #include "strstrea.h" // Register the present test cell CPPUNIT_TEST_SUITE_REGISTRATION (TestMabString); // define test cases void TestMabString :: findByName ( ) {// functional test, black box test // Normal test // condition and error test, belonging to white box test // extra test, // exception test, belight test // Exception test, Bool Bret = false Try {// put the exception code here ...} // catch (cxxx & e) catch (...) {Bret = true;} cppUnit_assert (BRET); // Because all unit tests should be executed The path, such as cmabstring is derived from the cstring // class, and the Find in cmabstring is simply called CS. The Find method in TRING, // does not need to be tested; // Mete all the paths that do not have tests; // Other test, see ...} void testmabstring: setup () {// Start test Initial code m_pnode = new node ();} void testmabstring :: teardown () {// test end code if (m_pnode) delete m_pnode;}