偶就结合折腾过程,写一个更详细点的helloworld。
系统:fc4,gcc4
(1)下载
到cppunit.sourceforge.net上下载源代码。我的是cppunit-1.10.2.tar.gz。copy到/usr/src下。运行:
tar -xf cppunit-1.10.2.tar.gz
解压缩。
(2)安装
进入cppunit-1.10.2目录下。运行:
./configure; make; make check; make install
安装。make check有些项通不过。没关系。:P
.o, .a文件已经安装到/usr/local/lib中去了,但头文件没安装到/usr/include中去
(3)copy *.h文件
把cppunit-1.10.2的cppunit目录复制到/usr/include下
(4)撰写mytest.cpp(从cppunit.sourceforge.net上copy下来的)
1 #include <iostream>
2
3 #include <cppunit/TestRunner.h>
4 #include <cppunit/TestResult.h>
5 #include <cppunit/TestResultCollector.h>
6 #include <cppunit/extensions/HelperMacros.h>
7 #include <cppunit/BriefTestProgressListener.h>
8 #include <cppunit/extensions/TestFactoryRegistry.h>
9
10
11 class Test : public CPPUNIT_NS::TestCase
12 {
13 CPPUNIT_TEST_SUITE(Test);
14 CPPUNIT_TEST(testHelloWorld);
15 CPPUNIT_TEST_SUITE_END();
16
17 public:
18 void setUp(void) {}
19 void tearDown(void) {}
20
21 protected:
22 void testHelloWorld(void) { std::cout << "Hello, world!" << std::endl; }
23 };
24
25 CPPUNIT_TEST_SUITE_REGISTRATION(Test);
26
27 int main( int argc, char **argv )
28 {
29 // Create the event manager and test controller
30 CPPUNIT_NS::TestResult controller;
31
32 // Add a listener that colllects test result
33 CPPUNIT_NS::TestResultCollector result;
34 controller.addListener( &result );
35
36 // Add a listener that print dots as test run.
37 CPPUNIT_NS::BriefTestProgressListener progress;
38 controller.addListener( &progress );
39
40 // Add the top suite to the test runner
41 CPPUNIT_NS::TestRunner runner;
42 runner.addTest( CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest() );
43 runner.run( controller );
44
45 return result.wasSuccessful() ? 0 : 1;
46 }
47
48
(5)编译,运行2
3 #include <cppunit/TestRunner.h>
4 #include <cppunit/TestResult.h>
5 #include <cppunit/TestResultCollector.h>
6 #include <cppunit/extensions/HelperMacros.h>
7 #include <cppunit/BriefTestProgressListener.h>
8 #include <cppunit/extensions/TestFactoryRegistry.h>
9
10
11 class Test : public CPPUNIT_NS::TestCase
12 {
13 CPPUNIT_TEST_SUITE(Test);
14 CPPUNIT_TEST(testHelloWorld);
15 CPPUNIT_TEST_SUITE_END();
16
17 public:
18 void setUp(void) {}
19 void tearDown(void) {}
20
21 protected:
22 void testHelloWorld(void) { std::cout << "Hello, world!" << std::endl; }
23 };
24
25 CPPUNIT_TEST_SUITE_REGISTRATION(Test);
26
27 int main( int argc, char **argv )
28 {
29 // Create the event manager and test controller
30 CPPUNIT_NS::TestResult controller;
31
32 // Add a listener that colllects test result
33 CPPUNIT_NS::TestResultCollector result;
34 controller.addListener( &result );
35
36 // Add a listener that print dots as test run.
37 CPPUNIT_NS::BriefTestProgressListener progress;
38 controller.addListener( &progress );
39
40 // Add the top suite to the test runner
41 CPPUNIT_NS::TestRunner runner;
42 runner.addTest( CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest() );
43 runner.run( controller );
44
45 return result.wasSuccessful() ? 0 : 1;
46 }
47
48
有两种方法
(a) 链接静态库。编译命令:
g++ -L/usr/local/lib/libcppunit.a mytest.cpp -lcppunit -ldl -o mytest
运行:
./mytest
结果:
Test::testHelloWorldHello, world!
: OK
(b) 链接动态库。编译命令:
g++ mytest.cpp -lcppunit -ldl -o mytest
运行时要先设置环境变量LD_LIBRARY_PATH到cppunit的安装目录,也就是/usr/local/lib,命令如下:
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
然后运行:
./mytest
结果:
Test::testHelloWorldHello, world!
: OK
延伸阅读
文章来源于领测软件测试网 https://www.ltesting.net/
领测软件测试网最新更新
关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:info@testage.com.cn 电话:010-51297073
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:info@testage.com.cn 电话:010-51297073