简体   繁体   English

我可以将BOOST_TEST_CASE用于非静态类成员函数吗?

[英]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.

相关问题 boost :: bind用于模板类中的非静态成员函数 - boost::bind for a non-static member function in a template class boost :: thread无效使用非静态成员函数 - boost::thread invalid use of a non-static member function How can I call a non-static member function of a class from a static member function of another class? - How can I call a non-static member function of a class from a static member function of another class? 非静态成员函数的无效使用 - 类成员函数调用另一个类成员函数 - Invalid use of non-static member function - Class Member Function Calling Another Class Member Function 如何将非静态成员函数传递给 ftw? - How can I pass a non-static member function to ftw? 无效使用非静态成员函数 - invalid use of non-static member function 将非静态成员 function 传递给另一个 class 的成员 function - Pass non-static member function to member function of another class boost::signals2 插槽作为非静态 function 成员? - boost::signals2 slot as a non-static function member? 非静态成员函数的无效使用在类模板的成员函数的实例化中? - Invalid use of non-static member function In instantiation of member function of a class template? 使用Boost绑定时无效使用非静态成员函数-C ++ - invalid use of non-static member function when using boost bind - c++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM