The unit test tool cppunit uses a graphical interface on the Windows platform for intuitive operations. However, it takes some time to configure the unit test tool on the UNIX platform:
1. download the file:
This user uses cppunit-1.12.0.rar version, can download in the following address (http://u.115.com/file/f6a03bec8) Sharing has a month, this software can be found online. If the above address has expired and the appropriate version is not found, please leave a message to contact;
2. directly extract cppunit to the specified path without installation;
3. Write cppunit makefile. You must specify the following 3 paths:
1. Waiting for testCodePath;
2. cppunit software path;
3. Test the code path;
Example of makefile:
Cc = CMV = mvcp = CPRM = rmcflags = # Change itcolin_home =/userhome/Colin Cppunit_home =/Userhome/Colin/tools/cppunit-1.12.0 Project_home = $ (Colin_home)/CL/src Unittest_home = $ (Colin_home)/CL/unittestoralib =-L $ (ORACLE_HOME)/lib32-lclntshcppunitlib =-L $ (cppunit_home)/lib-lcppunitorainc =-I $ (ORACLE_HOME) /rdbms/demo-I $ (ORACLE_HOME)/rdbms/public-I $ (ORACLE_HOME)/precomp/publiccxxflags =-O2-G #-wallappinc =-I $ (project_home) \-I $ (cppunit_home)/include \-I $ (unittest_home) clobj = $ (project_home)/common/record. O \ $ (project_home)/common/recordset. O \ $ (project_home)/common/dbhandler. O \ $ (project_home)/common/dbhandlerimpl. O \ $ (project_home)/common/logmacros. O \ $ (project_home)/common/datetime. O \ $ (project_home)/cltypeb/cltypebhandler. O \ $ (project_home)/cltypeb/cltypebupdate. ocltestobj = $ (unittest_home)/cltypebtest. O \ $ (unittest_home)/cltypebunittest. oobjs = $ (clobj) $ (cltestobj) All: cltestcltest: $ (objs) $ (CC)-o $ @ $ (objs) $ (cxxflags) $ (oralib) $ (cppunitlib ). suffixes :. CPP. CPP. o: $ (CC) $ (cflags) $ (appinc) $ (orainc)-c-o $ <-o $ *. o. phony: cleanclean: $ (RM) $ (objs)
4. Write the test code at $ (colin_home)/CL/unittest/. The test code consists of two parts:
1. Files with primary functions are in fixed format and do not need to be modified:
#include
# include
# include
# include
int main (INT argc, char * argv []) {// get the T OP level suite from the Registry cppunit: test * suite = cppunit: testfactoryregistry: getregistry (). maketest (); // adds the test to the list of test to run cppunit: textui: testrunner runner; runner. addtest (suite); // change the default outputter to a compiler error format outputter runner. setoutputter (New cppunit: compileroutputter (& runner. result (), STD: cerr); // run the tests. bool wassu Cessful = runner. Run (); // Return Error Code 1 if the one of test failed. Return wassucessful? 0: 1 ;}
2. unit test class classsvrplugintest: Some macros are added, which are similar to the MFC processing method. Add them as follows:
For each test case, you can write it into a function test1 () and add it to the macro cppunit_test:
Classsvrplugintest. h
# Pragma once # include <cppunit/extensions/helpermacros. h> using namespace STD; Class classsvrplugintest: Public cppunit: testfixture {cppunit_test_suite (classsvrplugintest); cppunit_test (test1); cppunit_test (Test2); principal (); public: classsvrplugintest (void );~ Classsvrplugintest (void); void test1 (); void Test2 ();};
Classsvrplugintest. cpp
# Include "classsvrplugintest. H" cppunit_test_suite_registration (classsvrplugintest); classsvrplugintest: classsvrplugintest (void) {} classsvrplugintest ::~ Classsvrplugintest (void) {} void classsvrvrintest: test1 () {int I =-1; cppunit_assert_equal (-1, I);} void classsvrplugintest: Test2 () {int I =-1; cppunit_assert_equal (-1, I );}
After the configuration is complete, makefile generates the executable file cltest for testing.
Of course, the above Code is not actually testedSource code, Classsvrplugintest. CPP contains the source code header, and then generates an object to test the return value of its function. cppunit uses the macro cppunit_assert_equal (-1, I) to determine whether the test result is the same as expected.
Over!