【问题标题】:Problem in using Boost Unit Test使用 Boost 单元测试的问题
【发布时间】:2011-09-05 10:48:57
【问题描述】:

我想开始使用 Boost Test 库为我的应用程序创建测试。

按照我在http://www.boost.org/doc/libs/1_47_0/libs/test/doc/html/tutorials/new-year-resolution.html 找到的教程,我开始了我的测试课程。

所以,我为我的测试创建了一个类,简单的 .cpp 就是这个

#define BOOST_TEST_MODULE MyClass test
#include <boost/test/unit_test.hpp>

#include "myclasstest.h"

MyClassTest::MyClassTest()
{

}

/**
 * Test the class.
 */
bool MyClassTest::testClass()
{
    BOOST_AUTO_TEST_CASE(empty_test)
    {
        MyClass xTest;
        BOOST_CHECK(xTest.isEmpty());
    }

return true;
}

好吧,我知道我必须做一些比 return true 更聪明的事情,但这不是问题。问题是它无法编译。 我认为该库已更正加载,因为如果我只编译前两行我没有错误,如教程页面中所述。

如果我尝试编译它,我会从 GCC 获得这个错误输出:

myclasstest.cpp: In member function ‘bool MyClassTest::testClass()’:
myclasstest.cpp:16:5: error: a function-definition is not allowed here before ‘{’ token
myclasstest.cpp:16:1: error: ‘empty_test_invoker’ was not declared in this scope
myclasstest.cpp:16:5: error: template argument for ‘template<class T> struct  boost::unit_test::ut_detail::auto_tc_exp_fail’ uses local type ‘MyClassTest::testClass()::empty_test_id’
myclasstest.cpp:16:5: error:   trying to instantiate ‘template<class T> struct boost::unit_test::ut_detail::auto_tc_exp_fail’
myclasstest.cpp:17:5: error: a function-definition is not allowed here before ‘{’ token
myclasstest.cpp:23:1: error: expected ‘}’ at end of input
myclasstest.cpp:23:1: warning: no return statement in function returning non-void

我是 Boost 新手,所以我不知道我必须做什么。我做错了什么?我认为我已经完成了相同的教程步骤,或者没有?

感谢您的回复。

【问题讨论】:

    标签: unit-testing gcc boost-test


    【解决方案1】:

    BOOST_AUTO_TEST_CASE 应该放在文件范围内。它不能放在函数实现中。您可以使用基于类方法的测试用例,但不能使用自动注册(暂时)。查看文档了解更多详情

    【讨论】:

      【解决方案2】:

      您应该将 BOOST_AUTO_TEST_CASE 与非成员函数一起使用。例如:

      #define BOOST_TEST_MODULE MyClass test
      #include <boost/test/unit_test.hpp>
      
      #include "MyClass.h"
      
      BOOST_AUTO_TEST_CASE( testMyClass )
      {
         MyClass xTest;
         BOOST_CHECK(xTest.isEmpty());
      }
      

      如果您需要测试上下文,请检查 fixtures

      【讨论】:

        猜你喜欢
        • 2014-08-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-01
        • 1970-01-01
        • 2011-01-06
        相关资源
        最近更新 更多