2011-12-13 107 views

回答

0

所有测试类的基类是CppUnit::TestFixture,您可以覆盖一些函数,如setUptearDown以初始化测试对象并将其删除。

考虑您有一个名为MyFirstTest,注册通过cpp框架的测试功能,测试类,你必须做的:

CPPUNIT_TEST_SUITE(MyFirstTest); 
CPPUNIT_TEST(myTestFunction); 
... //any other function you want to register with appropriate macros 
CPPUNIT_TEST_SUITE_END(); 

你也必须注册每个测试类(在它们各自的头或CPP文件)

CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(MyFirstTest, "YouTestName"); 

一旦你的测试类被设置,你可以运行它。主要功能如下:

bool wasSuccessful = false; 

    try 
    { 
     CppUnit::TextUi::TestRunner runner; 
     runner.setOutputter(new CppUnit::CompilerOutputter(&runner.result(), std::cerr)); 
     CppUnit::TestFactoryRegistry &registry = CppUnit::TestFactoryRegistry::getRegistry("YouTestName"); 
     runner.addTest(registry.makeTest()); 
     wasSuccessful = runner.run("", false); 
    } 
    catch(const std::exception& e) 
    { 
     std::cerr << e.what() << std::endl; 
     wasSuccessful = false; 
    } 

如果您希望添加更多的测试类,主函数将保持不变。您只需创建测试类(来自CppUnit::TestFixture类),注册您的方法,重要的一步是使用CPPUNIT_TEST_SUITE_NAMED_REGISTRATION注册您的框架类。在main函数中使用的getRegistry方法将获得您向框架注册的所有测试类,并执行您使用CPPUNIT_TEST或任何其他适当的宏注册的那些类的所有方法。

+0

那么,为什么他们会显示这样一个简单的TestCase,如果没有所有的基础设施,它似乎不能运行?我会考虑给他们说什么,应该可以运行一个TestCase – dschatz

+0

其实他们已经使用了两种方法我想。一个是我刚才提到的,另一个是注册你的方法与测试西装类,然后运行每个套件。 – ata

0

你所指的页面描述了整个过程,包括很多关于如何在TestFixtures中手动编写代码的额外内容,以及如何在TestSuite中注册这些代码,以及如何使用宏写和登记他们,这是非常罗嗦。有时候最好只向人们展示一个简单的例子。他们在页面的最底部有这个:

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

int main(int argc, char **argv) 
{ 
    CppUnit::TextUi::TestRunner runner; 
    CppUnit::TestFactoryRegistry &registry = CppUnit::TestFactoryRegistry::getRegistry(); 
    runner.addTest(registry.makeTest()); 
    bool wasSuccessful = runner.run("", false); 
    return wasSuccessful; 
} 

基础设施非常简单,真的。您可以创建一个测试运行器,然后检索已注册测试的列表,将它们添加到运行器中,让运行器运行测试并报告给您。但是,最好让事情变得简单。人们不想做很难的事情。

2

我收集你要的是一个CppUnit的SSCCE。由于CppUnit是一个框架,所以一个最小的例子必须放置最小的测试结构 - 就像一个TestFixture,否则一个可能没有整个CppUnit,只是使用std::assert。所有这一切都可以在一个文件中完成,比如以下形式的Main.cpp

//Declaration file: MTest.h 
#ifndef MTEST_H 
#define MTEST_H 
#include <cppunit/extensions/HelperMacros.h> 

class MTest : public CPPUNIT_NS::TestFixture 
{ 
    CPPUNIT_TEST_SUITE(MTest); 
    CPPUNIT_TEST(simpleTest); 
    CPPUNIT_TEST_SUITE_END(); 
public: 
    void simpleTest(); 
}; 
#endif // MTEST_H 

////////////////////////////////////// 
// Implementation file, e.g. MTest.cpp 
#include <cppunit/config/SourcePrefix.h> 
//#include "MTest.h" 

// Registers the fixture into the 'registry' 
CPPUNIT_TEST_SUITE_REGISTRATION(MTest); 

// Some code to be tested. 
void MTest::simpleTest() { 
    CPPUNIT_ASSERT_EQUAL(1, 2); 
} 

///////////////////////////////////// 
// Main file, Main.cpp 
#include <cppunit/extensions/TestFactoryRegistry.h> 
#include <cppunit/ui/text/TestRunner.h> 

int main(int argc, char* argv[]) 
{ 
    CPPUNIT_NS::TextUi::TestRunner runner; //the runner 
    // Get the top level suite from the registry 
    CPPUNIT_NS::Test* suite = 
     CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest(); 
    // Adds the test to the list of test to run 
    runner.addTest(suite); 
    // Run the test. 
    bool wasSucessful = runner.run(); 
    // Return error code 1 if the one of test failed. 
    return wasSucessful ? 0 : 1; 
} 

这将需要编译/与cppunit库例如链接g++ Main.cpp ../../src/cppunit/.libs/libcppunit.a(如果您恰好在库的主目录下启动2个级别[根据环境要求插入libcppunit库的静态或动态版本])。

A “更清洁” 的examply将分裂码成单独MTest.h.cpp,所指示的)和Main.cpp。在这种情况下,来自Main.cpp的CppUnit方法会调用MTest文件中由CppUnit助手宏提供的方法。因此它们应该连接在一起,例如由g++ MTest.o Main.o ../../src/cppunit/.libs/libcppunit.a