[英]Can I use BOOST_TEST_CASE for non-static class member function?
I am now using BOOST::UNIT_TEST framework to perform unit test, and it is a great tool to perform unit test. 我现在使用BOOST :: UNIT_TEST框架来执行单元测试,它是执行单元测试的好工具。 However, I found something inconvenient: when I add functions to test, it seems that the functions must be static or a free function as the following codes illustrate: 但是,我发现了一些不方便的东西:当我添加函数进行测试时,似乎函数必须是静态函数或自由函数,如下面的代码所示:
test_suite* ts = BOOST_TEST_SUITE( "unit_test" );
ts->add(BOOST_TEST_CASE(&static_fun));
When it is not a static function, for example, 例如,当它不是静态函数时,
class Abc
{
public:
void myfun();
};
Abc obj;
ts->add(BOOST_TEST_CASE(&(obj.myfun)));
then, I will have C2064: term does not evaluate to a function taking 0 arguments
error. 那么,我将有C2064: term does not evaluate to a function taking 0 arguments
错误C2064: term does not evaluate to a function taking 0 arguments
。 Any idea that I can add the non-static class member function to the test framework? 我是否可以将非静态类成员函数添加到测试框架? Thanks 谢谢
I think you try to define a test fixture class? 我想你试着定义一个测试夹具类? The way I use boost test in most cases looks like this: 在大多数情况下我使用boost测试的方式如下所示:
struct MyTestFixture
{
int test_data;
void func()
{
// some common test code
}
};
BOOST_FIXTURE_TEST_SUITE(MyTest, MyTestFixture)
BOOST_AUTO_TEST_CASE(Test1)
{
test_data = 3;
func();
// ...
}
BOOST_AUTO_TEST_CASE(Test2)
{
// ...
}
BOOST_AUTO_TEST_SUITE_END()
你应该看看这里你可以使用boost::bind
来实现这种情况。
ts->add(BOOST_TEST_CASE(boost::bind(&Abc::myfun, obj)));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.