C++单元测试之四--利用cppunit运行测试用例

  1. 云栖社区>
  2. 博客>
  3. 正文

C++单元测试之四--利用cppunit运行测试用例

罗宾豪 2012-09-21 08:44:44 浏览510
展开阅读全文
cppunit是一个很好的针对于C++/C的单元测试框架,它源自于junit。利用cppunit我们可以很好的管理运行测试用例。
cppunit是开源的,遵循LGPLv2许可证。
你可以从http://sourceforge.net/projects/cppunit/下载到最新版本。
下载解压后依次运行:
./configure
./make
./make install
cppunit就会被自动部署到你的机器上,其中头文件位于/usr/include/cppunit目录下,库文件位于/usr/local/lib中。你也可以直接把附件中编译好的cppunit解压拷贝到对应目录下。

设置好cppunit环境后,下面就开始修改代码。
首先修改Main.cpp如下:

点击(此处)折叠或打开

 #include stdio.h>
  1. #include stdlib.h>
  2. #include cppunit/extensions/TestFactoryRegistry.h>
  3. #include cppunit/ui/text/TestRunner.h>

  4. int main(int argc, char *argv[])
  5. {
  6.         CppUnit::TextUi::TestRunner runner;
  7.         CppUnit::TestFactoryRegistry &registry = CppUnit::TestFactoryRegistry::getRegistry();
  8.         runner.addTest(registry.makeTest());

  9.         runner.run();


  10.     return 0;
  11. }
其次添加TestA.cpp/.h来测试A.cpp:

点击(此处)折叠或打开

  1. //TestA.h
  2. #include cppunit/extensions/HelperMacros.h>

  3. class TestA: public CppUnit::TestFixture{
  4.         CPPUNIT_TEST_SUITE(TestA);
  5.         CPPUNIT_TEST(testSum);
  6.         CPPUNIT_TEST(testMultiply);
  7.         CPPUNIT_TEST_SUITE_END();
  8. public:
  9.         void testSum();
  10.         void testMultiply();
  11. };


点击(此处)折叠或打开

  1. //TestA.cpp
  2. #include "A.h"
  3. #include "TestA.h"
  4. CPPUNIT_TEST_SUITE_REGISTRATION(TestA);
  5. void TestA::testSum()
  6. {
  7.         A a;
  8.         CPPUNIT_ASSERT(11 == a.Sum(5,6));

  9. }

  10. void TestA::testMultiply()
  11. {
  12.         A a;
  13.         CPPUNIT_ASSERT(30 == a.Multiply(5,6));
  14. }

cppunit使用断言(ASSERT)来判断测试结果是否正确。
最后修改Makefile:


点击(此处)折叠或打开

  1. OBJS= Main.o A.o TestA.o

  2. UTest.out: $(OBJS)
  3.         g++ -o UTest.out $(OBJS) -ldl -lgcov -lcppunit


  4. CURDIR=$(shell pwd)

  5. $(OBJS): %.o: %.cpp
  6.         g++ -I$(CURDIR) -c -g -fprofile-arcs -ftest-coverage $ -o $@

  7. #unit test and code coverage check
  8. UT:UTest.out
  9.         valgrind --tool=memcheck --leak-check=full ./UTest.out
  10.         ./gcov_checker 90 --objects $(OBJS)
  11. #gcov -o A.o A.cpp

  12. .PHONY: clean
  13. clean:
  14.         rm -f *.o *.gcno *.gcda *.gcov UTest.out

编译运行的结果是:

点击(此处)折叠或打开

  1. [root@tivu25 utcov_cppunit]# make clean;make UT
  2. rm -f *.o *.gcno *.gcda *.gcov UTest.out
    g++  -I/home/haoqf/src/UTest/utcov_cppunit -c -g -fprofile-arcs -ftest-coverage Main.cpp -o Main.o
    g++  -I/home/haoqf/src/UTest/utcov_cppunit -c -g -fprofile-arcs -ftest-coverage A.cpp -o A.o
    g++  -I/home/haoqf/src/UTest/utcov_cppunit -c -g -fprofile-arcs -ftest-coverage TestA.cpp -o TestA.o
    g++ -o UTest.out Main.o A.o TestA.o  -ldl -lgcov -lcppunit
    valgrind --tool=memcheck --leak-check=full ./UTest.out
    ==31409== Memcheck, a memory error detector.
    ==31409== Copyright (C) 2002-2006, and GNU GPL'd, by Julian Seward et al.
    ==31409== Using LibVEX rev 1658, a library for dynamic binary translation.
    ==31409== Copyright (C) 2004-2006, and GNU GPL'd, by OpenWorks LLP.
    ==31409== Using valgrind-3.2.1, a dynamic binary instrumentation framework.
    ==31409== Copyright (C) 2000-2006, and GNU GPL'd, by Julian Seward et al.
    ==31409== For more details, rerun with: -v
    ==31409==
    ..


    OK (2 tests)


    ==31409==
    ==31409== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 19 from 1)
    ==31409== malloc/free: in use at exit: 0 bytes in 0 blocks.
    ==31409== malloc/free: 82 allocs, 82 frees, 9,807 bytes allocated.
    ==31409== For counts of detected errors, rerun with: -v
    ==31409== All heap blocks were freed -- no leaks are possible.
    ./gcov_checker 90 --objects Main.o A.o TestA.o
    Code coverage:
    Main.cpp:100.0%
    A.cpp:100.0%
    TestA.cpp:100.0%
    See *.gcov files for details.

zip.gif cppunit_compiled.zip  
《返璞归真--UNIX技术内幕》在全国各大书店及网城均有销售:
京东
亚马逊                          China pub
上学吧                          1号店




网友评论

登录后评论
0/500
评论
罗宾豪
+ 关注