(6)编写第一个HelloWorld
#include <iostream>
#include <cppunit/TestRunner.h>
#include <cppunit/TestResult.h>
#include <cppunit/TestResultCollector.h>
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/BriefTestProgressListener.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
class Test : public CPPUNIT_NS::TestCase
{
CPPUNIT_TEST_SUITE(Test);
CPPUNIT_TEST(testHelloWorld);
CPPUNIT_TEST_SUITE_END();
public:
void setUp(void) {}
void tearDown(void) {}
protected:
void testHelloWorld(void) { std::cout << "Hello, world!" << std::endl; }
};
CPPUNIT_TEST_SUITE_REGISTRATION(Test);
int main( int argc, char **argv )
{
// Create the event manager and test controller
CPPUNIT_NS::TestResult controller;
// Add a listener that colllects test result
CPPUNIT_NS::TestResultCollector result;
controller.addListener( &result );
// Add a listener that print dots as test run.
CPPUNIT_NS::BriefTestProgressListener progress;
controller.addListener( &progress );
// Add the top suite to the test runner
CPPUNIT_NS::TestRunner runner;
runner.addTest( CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest() );
runner.run( controller );
return result.wasSuccessful() ? 0 : 1;
}
将之保存为mytest.cpp。
(7)编译我们的HelloWorld
你可以链接静态库也可以链接静态库。
(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
运行:
./mytest
结果:
Test::testHelloWorldHello, world!
: OK
如果你没有执行步骤(5),那么你也可以在每次运行之前设置下临时的环境变量LD_LIBRARY_PATH命令如下:
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
撰写mytest.cpp(从cppunit.sourceforge.net上copy下来的),代码如下:
#include <iostream>
#include <cppunit/TestRunner.h>
#include <cppunit/TestResult.h>
#include <cppunit/TestResultCollector.h>
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/BriefTestProgressListener.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
class Test : public CPPUNIT_NS::TestCase
{
CPPUNIT_TEST_SUITE(Test);
CPPUNIT_TEST(testHelloWorld);
CPPUNIT_TEST_SUITE_END();
public:
void setUp(void) {}
void tearDown(void) {}
protected:
void testHelloWorld(void) { std::cout << "Hello, world!" << std::endl; }
};
CPPUNIT_TEST_SUITE_REGISTRATION(Test);
int main( int argc, char **argv )
{
// Create the event manager and test controller
CPPUNIT_NS::TestResult controller;
// Add a listener that colllects test result
CPPUNIT_NS::TestResultCollector result;
controller.addListener( &result );
// Add a listener that print dots as test run.
CPPUNIT_NS::BriefTestProgressListener progress;
controller.addListener( &progress );
// Add the top suite to the test runner
CPPUNIT_NS::TestRunner runner;
runner.addTest( CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest() );
runner.run( controller );
return result.wasSuccessful() ? 0 : 1;
}
将之保存为mytest.cpp。
(7)编译我们的HelloWorld
你可以链接静态库也可以链接静态库。
(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
运行:
./mytest
结果:
Test::testHelloWorldHello, world!
: OK
如果你没有执行步骤(5),那么你也可以在每次运行之前设置下临时的环境变量LD_LIBRARY_PATH命令如下:
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
延伸阅读
文章来源于领测软件测试网 https://www.ltesting.net/