C++单元测试之四--利用cppunit运行测试用例
2012-09-21 08:44:44
cppunit是一个很好的针对于C++/C的单元测试框架,它源自于junit。利用cppunit我们可以很好的管理运行测试用例。 cppunit是开源的,遵循LGPLv2许可证。 你可以从http:///projects/cppunit/下载到最新版本。 下载解压后依次运行: ./configure ./make ./make install cppunit就会被自动部署到你的机器上,其中头文件位于/usr/include/cppunit目录下,库文件位于/usr/local/lib中。你也可以直接把附件中编译好的cppunit解压拷贝到对应目录下。
设置好cppunit环境后,下面就开始修改代码。 首先修改Main.cpp如下:
#include <stdio .h >- #include <stdlib.h>
- #include <cppunit/extensions/TestFactoryRegistry.h>
- #include <cppunit/ui/text/TestRunner.h>
- int main(int argc, char *argv[])
- {
- CppUnit::TextUi::TestRunner runner;
- CppUnit::TestFactoryRegistry ®istry = CppUnit::TestFactoryRegistry::getRegistry();
- runner.addTest(registry.makeTest());
- runner.run();
- return 0;
- }
其次添加TestA.cpp/.h来测试A.cpp:
- //TestA.h
- #include <cppunit/extensions/HelperMacros.h>
- class TestA: public CppUnit::TestFixture{
- CPPUNIT_TEST_SUITE(TestA);
- CPPUNIT_TEST(testSum);
- CPPUNIT_TEST(testMultiply);
- CPPUNIT_TEST_SUITE_END();
- public:
- void testSum();
- void testMultiply();
- };
- //TestA.cpp
- #include "A.h"
- #include "TestA.h"
- CPPUNIT_TEST_SUITE_REGISTRATION(TestA);
- void TestA::testSum()
- {
- A a;
- CPPUNIT_ASSERT(11 == a.Sum(5,6));
- }
- void TestA::testMultiply()
- {
- A a;
- CPPUNIT_ASSERT(30 == a.Multiply(5,6));
- }
cppunit使用断言(ASSERT)来判断测试结果是否正确。 最后修改Makefile:
- OBJS= Main.o A.o TestA.o
- UTest.out: $(OBJS)
- g++ -o UTest.out $(OBJS) -ldl -lgcov -lcppunit
- CURDIR=$(shell pwd)
- $(OBJS): %.o: %.cpp
- g++ -I$(CURDIR) -c -g -fprofile-arcs -ftest-coverage $< -o $@
- #unit test and code coverage check
- UT:UTest.out
- valgrind --tool=memcheck --leak-check=full ./UTest.out
- ./gcov_checker 90 --objects $(OBJS)
- #gcov -o A.o A.cpp
- .PHONY: clean
- clean:
- rm -f *.o *.gcno *.gcda *.gcov UTest.out
编译运行的结果是:
- [root@tivu25 utcov_cppunit]# make clean;make UT
- 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.
|