首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Boost单元测试时出现的问题

使用Boost单元测试时出现的问题
EN

Stack Overflow用户
提问于 2011-09-05 18:48:57
回答 2查看 1.5K关注 0票数 0

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

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

因此,我为我的测试创建了一个类,简单的.cpp如下所示

代码语言:javascript
复制
#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;
}

好吧,我知道我必须做一些比返回true更聪明的事情,但这不是问题所在。问题是它不能编译。我认为这个库是正确加载的,因为如果我只编译前两行,就没有错误,正如教程页面中所解释的那样。

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

代码语言:javascript
复制
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的新手,所以我不知道我必须做什么。我哪里做错了?我想我已经完成了教程中相同的步骤,还是没有?

感谢您的回复。

EN

回答 2

Stack Overflow用户

发布于 2011-11-30 01:08:51

BOOST_AUTO_TEST_CASE应该放在文件作用域中。它不能放在函数实现中。您可以使用基于类方法的测试用例,但不能使用自动注册(暂时)。有关更多详细信息,请查看文档

票数 0
EN

Stack Overflow用户

发布于 2012-02-22 04:52:48

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

代码语言:javascript
复制
#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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7306962

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档