Boost 测试库使用不同的“参数”运行套件两次

发布于 2024-11-16 05:47:28 字数 143 浏览 3 评论 0原文

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

boost 测试库中是否有某种方法可以实现此目的,而无需为所有单独的测试用例编写包装器?

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

I have a suite of tests that can be run in a few different modes. Other than some global configuration, or a fixture config, the test case code is the same.

Is there some way in the boost test library to achieve this without having to write a wrapper around all the individual test cases?

Note this is not to be a command-line switch, it should be part of the same execution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

余罪 2024-11-23 05:47:28

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

他们还有 测试用例模板并且确实具有自动注册功能,因此如果配置数量不是太多,则可以通过为每个配置定义类型来滥用它。

编辑:测试用例模板可以使用如下所示:

// 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 ¶m>
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
}

The unary function test case is probably what you want. The only downside is that automatic registration (could be based on some kind of factory function) does not seem to be supported for it.

They also have test case template and that does have automatic registration, so it would be possible to abuse it by defining type for each configuration if there's not too many of them.

Edit: The test case template could be used something like this:

// 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 ¶m>
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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文