类,BOOST_TEST_MODULE,BOOST_AUTO_TEST_SUITE:命名问题

class, BOOST_TEST_MODULE, BOOST_AUTO_TEST_SUITE: problems from naming?

本文关键字:TEST BOOST 问题 MODULE AUTO SUITE      更新时间:2023-10-16

因此,我已经开始使用boost单元测试。当我尝试构建一个创建类实例的简单测试时,我会遇到编译错误。它在没有类实例的情况下运行良好。

编译错误消息为:

/src/test/WTFomgFail_test.cpp: In member function ‘void WTFomgFail::defaultConstructor::test_method()’:
/src/test/WTFomgFail_test.cpp:20: error: expected primary-expression before ‘obj’
/src/test/WTFomgFail_test.cpp:20: error: expected `;' before ‘obj’

WTFomgFail_test.cpp:

#include "WTFomgFail.hpp"
#define BOOST_TEST_MODULE WTFomgFail
#define BOOST_TEST_MAIN
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>
BOOST_AUTO_TEST_SUITE(WTFomgFail)
BOOST_AUTO_TEST_CASE( defaultConstructor )
{
    WTFomgFail obj = WTFomgFail();
    BOOST_MESSAGE("wow, you did it");
}
BOOST_AUTO_TEST_SUITE_END()

WTFomgFail.hpp:

#ifndef WTFOMGFAIL_HPP_
#define WTFOMGFAIL_HPP_
class WTFomgFail
{
public:
    WTFomgFail();
    ~WTFomgFail();
};
#endif /* WTFOMGFAIL_HPP_ */

WTFomgFail.cpp:

#include "WTFomgFail.hpp"
WTFomgFail::WTFomgFail()
{
}
WTFomgFail::~WTFomgFail()
{
}

如果我将BOOST_AUTO_TEST_SUITE(WTFomgFail)更改为其他内容,比如BOOST_AUTO_TEST_SUITE(OMGreally),错误就会消失。

此外,当使用#define BOOST_TEST_MODULE OMGreallyBOOST_AUTO_TEST_SUITE(OMGreally)时,我没有得到错误。

所以,我的问题是,当使用boost UTF时,将模块、testronguite和类命名为明确禁止的相同内容?

我知道我来晚了,但我只是偶然发现了它,它看起来很孤独。。。

要理解此限制,您必须了解Boost Tests最初是如何工作的。(它仍然可以这样工作,但当时没有BOOST_AUTO_...宏,必须这样做。)

来自文档:

class test_class {
public:
    void test_method1()
    {
        BOOST_CHECK( true /* test assertion */ );
    }
    void test_method2()
    {
        BOOST_CHECK( false /* test assertion */ );
    }
};
//____________________________________________________________________________//
test_suite*
init_unit_test_suite( int argc, char* argv[] ) 
{
    boost::shared_ptr<test_class> tester( new test_class );
    framework::master_test_suite().
        add( BOOST_TEST_CASE( boost::bind( &test_class::test_method1, tester )));
    framework::master_test_suite().
        add( BOOST_TEST_CASE( boost::bind( &test_class::test_method2, tester )));
    return 0;
}

这有点麻烦,因为每次添加测试函数时,都必须在两个不同的位置更改代码(函数的定义和向测试套件注册)。注册也有点不直观。

这就是为什么他们推出了BOOST_AUTO_TEST_SUITEBOOST_AUTO_TEST_CASE,它们正在为您做这件事。

传递给BOOST_AUTO_TEST_SUITE的参数当然是类的名称(上面的test_class)。BOOST_AUTO_TEST_CASE的参数是测试函数的名称(上面的test_method1()test_method2())。

所以不,这些可能(当然)与您正在测试的类和函数不相同。您可以为此使用名称空间,但就我个人而言,我更喜欢简单地将类名后缀为Tu(如果您不喜欢CamelCase命名,则为_tu),并将其用于测试套件。

我希望这能有所帮助。