cppunit使用指南带makefile文件

 转载自: http://www.cnblogs.com/me115/archive/2011/05/04/2036820.html

单元测试工具CPPUnit在Windows平台下使用图形界面,操作非常直观;但在Unix平台下,就需要花点功夫配置一番:

1.下载文件:

本人使用的是cppunit-1.12.0.rar 版本,可以在以下地址下载(http://u.115.com/file/f6a03bec8)共享有效期有一个月,这软件在网上也好找。如以上地址过期,未找到合适版本,请留言联系;

2.不用安装,直接将cppunit解压到指定路径;

3.编写CPPUnit makefile, 需指定以下3中路径:

1.待测试代码路径;

2.CPPUNIT软件路径;

3.测试代码路径;

给出makefile范例:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

CC      =      CC

MV      =       mv

CP      =       cp

RM      =       rm

CFLAGS  =

  

# Change It

COLIN_HOME =/userhome/colin

<STRONG>CPPUNIT_HOME</STRONG> = /userhome/colin/tools/cppunit-1.12.0

<STRONG>PROJECT_HOME</STRONG> = $(COLIN_HOME)/cl/src

<STRONG>UNITTEST_HOME</STRONG>= $(COLIN_HOME)/cl/unittest

  

ORALIB = -L$(ORACLE_HOME)/lib32 -lclntsh

CPPUNITLIB = -L$(CPPUNIT_HOME)/lib -lcppunit

ORAINC = -I$(ORACLE_HOME)/rdbms/demo -I$(ORACLE_HOME)/rdbms/public -I$(ORACLE_HOME)/precomp/public

CXXFLAGS = -O2 -g #-Wall

  

APPINC = -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.o

  

  

clTESTOBJ =  $(UNITTEST_HOME)/clTypeBTest.o \

             $(UNITTEST_HOME)/clTypeBUnitTest.o

  

OBJS = $(clOBJ) $(clTESTOBJ)

  

all: cltest

  

cltest: $(OBJS)

        $(CC) -o $@  $(OBJS) $(CXXFLAGS) $(ORALIB) $(CPPUNITLIB)

  

.SUFFIXES : .cpp

.cpp.o :

        $(CC) $(CFLAGS) $(APPINC) $(ORAINC) -c -O $< -o $*.o    

  

.PHONY:clean

clean:

        $(RM) $(OBJS)

 

4.在$(COLIN_HOME)/cl/unittest/编写测试代码,测试代码由两部分组成:

1.带主函数的文件,固定格式,不用修改:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

#include <cppunit/extensions/HelperMacros.h>

#include <cppunit/CompilerOutputter.h>

#include <cppunit/extensions/TestFactoryRegistry.h>

#include <cppunit/ui/text/TestRunner.h>

  

int main(int argc, char* argv[])

{

    // Get the top 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 wasSucessful = runner.run();

  

    // Return error code 1 if the one of test failed.

    return wasSucessful ? 0 : 1;

}

2.单元测试类ClassSvrPluginTest:其中加入了一些宏,类似于MFC处理方式,按照例子相应的加入:

对于每个测试用例,可写成一个函数test1(),并加入到宏CPPUNIT_TEST()中:

ClassSvrPluginTest.h

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

#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);

    CPPUNIT_TEST_SUITE_END();

public:

    ClassSvrPluginTest(void);

    ~ClassSvrPluginTest(void);

    void test1();

    void test2();

};

ClassSvrPluginTest.cpp

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

#include "ClassSvrPluginTest.h"

  

CPPUNIT_TEST_SUITE_REGISTRATION(ClassSvrPluginTest);

  

ClassSvrPluginTest::ClassSvrPluginTest(void)

{

}

  

ClassSvrPluginTest::~ClassSvrPluginTest(void)

{

}

  

void ClassSvrPluginTest::test1()

{

    int i = -1;

    CPPUNIT_ASSERT_EQUAL(-1, i);

}

  

void ClassSvrPluginTest::test2()

{

    int i = -1;

    CPPUNIT_ASSERT_EQUAL(-1, i);

}

配置完毕,makefile生成文件可执行文件cltest即可进行测试。

当然,上述代码并没有实际的测试源代码,可以ClassSvrPluginTest.cpp中包含源代码的头,然后,生成对象,对其函数的返回值进行测试,cppunit 通过宏CPPUNIT_ASSERT_EQUAL(-1, i)来判断测试结果是否和预期相同。

over!

 

你可能感兴趣的:(cppunit使用指南带makefile文件)