2012-10-22 103 views
0

通过阅读CppUnit食谱和大量的谷歌搜索之后,我一直无法弄清楚我得到的特定错误的原因。使用CPPUNIT_TEST_SUITE宏时出错

我有一个非常基本的CppUnit testFixture类 - > 我有一个文件 - MyTest.h,只有一个TestFixture类定义。

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

class MyTest : public CppUnit::TestFixture 
{ 
    CPPUNIT_TEST_SUITE(MyTest); // Line num 8 
    CPPUNIT_TEST(TestFailure); 
    CPPUNIT_TEST_SUITE_END(); 

    public: 
    void TestFailure() 
    { 
     CPPUNIT_ASSERT(false); 
    } 
}; 

另外,MyTest.cpp用于驱动这个MyTest类。

// MyTest.cpp 
#include "MyTest.h" 

然后,一个名为main.cpp的文件将实例化runner并运行实际的testcase。

// main.cpp 

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

// In my main, I define a macro ADD_TEST and do #include of file called "testList.h" 
// So my testList.h can have any number of ADD_TEST macros. 
int main(int argc, char **argv) 
{ 
     CppUnit::TextUi::TestRunner runner; 

     #define ADD_TEST(_testName) \ 
       runner.addTest(_testName::suite()); 
     #include testList.h" 
     #undef ADD_TEST 

     runner.run(); 
     return true; 
    } 

这里是我的testList.h - >

#pragma once 
#include MyTest.h 

ADD_TEST(MyTest) 

现在,这个文件结构的工作 - 这是在Windows安装程序。 在linux中,我获得以下奇怪的错误 -

MyTest.h: In function 'int main(int, char**)': MyTest.h:8: error: 'main(int, char**)::MyTest' uses local type 'main(int, char**)::MyTest' 
MyTest.h:8: error: trying to instantiate 'template<class Fixture> class CppUnit::TestSuiteBuilderContext' 
MyTest.h: In static member function 'static void main(int, char**)::MyTest::addTestsToSuite(CppUnit::TestSuiteBuilderContextBase&)': 
MyTest.h:8: error: cannot convert 'CppUnit::TestSuiteBuilderContextBase' to 'int' in initialization 

这有我彻底糊涂了。我知道这些宏正在被拾取,因为如果我在MyTest.h中注释掉第num8行,那么会出现“suite”未声明的错误。 但是,然后是CPPUNIT_TEST_SUITE等宏可用,那么为什么错误? 我正在编译-lstC++,-ldl & -lcppunit标志。

任何帮助表示赞赏!

谢谢!

回答

0

我还没有确定您的具体问题,但我确实找出了您可能考虑的事项。您的ADD_TEST似乎是一种非常手动的方式来处理CppUnit测试注册表旨在为您执行的操作。调用CPPUNIT_TEST_SUITE(MyTest)的原因;宏是在框架中注册你的测试,这样你就可以在运行时找到它们。

考虑,而不是一个主要的,看起来像这样:

int main(int argc, char **argv) 
{ 
    CppUnit::TextUi::TestRunner runner; 
    CppUnit::Test *test = CppUnit::TestFactoryRegistry::getRegistry().makeTest(); 

    runner.addTest(test); 
    runner.run(); 
    return true; 
} 

如果你想获得幻想,并提供不同的选择为哪个测试运行,你可以通过特定的测试名作为参数来选择他们getRegistry,如getRegistry("MyTest").makeTest();显然,这可能很容易被命令行或配置文件驱动,但是您想实现和控制它。