4

我有一套可以在几种不同模式下运行的测试。除了一些全局配置或夹具配置外,测试用例代码是相同的。

boost 测试库中是否有某种方法可以实现这一点,而不必为所有单独的测试用例编写包装器?

请注意,这不是命令行开关,它应该是同一执行的一部分。

4

1 回答 1

4

元函数测试用例可能是您想要的。唯一的缺点是它似乎不支持自动注册(可能基于某种工厂功能)。

他们也有测试用例模板并且确实有自动注册,所以如果没有太多配置,就有可能通过为每个配置定义类型来滥用它。

编辑:测试用例模板可以这样使用:

// Parameter is the type of parameter you need. Might be anything from simple int (in
// which case the template parameter may be a value, not reference) to complex object.
// It just has to be possible to create (static) global instances of it.

template <const Parameter &param>
struct Fixture {
    // do whatever you want, param is normal object reference here
    // it's not a member, but you can:
    const Parameter &getParameter() { return param; }
}

static Parameter p1(whatever);
static Parameter p2(something_else);
// ...

typedef boost::mpl::list<Fixture<p1>, Fixture<p2> > Fixtures;

BOOST_AUTO_TEST_CASE_TEMPLATE(test, F, Fixtures)
{
    F fixture; // Unfortunately you can't make it true fixture, so you have to have instance
    // Test what you want
}
于 2011-06-21T13:52:30.767 回答