每个套件的升压测试初始化​​(不是案例)

发布于 2024-12-21 01:02:00 字数 163 浏览 3 评论 0原文

我需要初始化一些变量,这些变量在 BOOST_AUTO_TEST_SUITE 中是“全局”的 因此,它们的构造函数将在套件启动时被调用,而它们的析构函数将在最后一个相应的 BOOST_AUTO_TEST_CASE 完成后立即被调用,

有人知道我该怎么做吗?看来全球赛程不是一个解决方案......

I need to init some variables, which are "global" inside a BOOST_AUTO_TEST_SUITE
so their constructors will be called when the suite starts and their destructors will be called right after the last corresponding BOOST_AUTO_TEST_CASE is finished

does someone know how I can do it? Looks like global fixtures is not a solution...

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

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

发布评论

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

评论(4

妄断弥空 2024-12-28 01:02:01

我认为 Boost 测试库不可能做到这一点。全局装置实际上是全局的,即它们在每次测试运行时实例化,而不是按套件实例化。

除此之外,我认为这样的设置会违反测试隔离原则。您能解释一下为什么在套件范围内需要“全局”变量吗?

I don't think it's possible with the Boost Test Library. Global fixtures are really global, i.e. they are instantiated per test run, not per suite.

Apart from that, I think that such a setup would violate test isolation principles. Can you explain why you need "global" variables at the suite scope?

当梦初醒 2024-12-28 01:02:01

您可以使用全局装置:
http:// www.boost.org/doc/libs/1_66_0/libs/test/doc/html/boost_test/tests_organization/fixtures/global.html

只是替换

BOOST_FIXTURE_TEST_SUITE( s, F )
BOOST_AUTO_TEST_CASE( test_case1 )
[...]
BOOST_AUTO_TEST_SUITE_END()

BOOST_TEST_GLOBAL_FIXTURE( F );
BOOST_AUTO_TEST_CASE( test_case1 )
[...]

那么它就会像你期望的那样工作。

--
符号39

You can use global fixtures:
http://www.boost.org/doc/libs/1_66_0/libs/test/doc/html/boost_test/tests_organization/fixtures/global.html

just replace

BOOST_FIXTURE_TEST_SUITE( s, F )
BOOST_AUTO_TEST_CASE( test_case1 )
[...]
BOOST_AUTO_TEST_SUITE_END()

with

BOOST_TEST_GLOBAL_FIXTURE( F );
BOOST_AUTO_TEST_CASE( test_case1 )
[...]

Then it will work like you expect.

--
sym39

情域 2024-12-28 01:02:00

我不太确定接受的答案是否正确,因为如果我使用 来自 boost 网站的测试代码

#define BOOST_TEST_MODULE example
#include <boost/test/included/unit_test.hpp>

struct F {
    F() : i( 0 ) { BOOST_TEST_MESSAGE( "setup fixture" ); }
    ~F()         { BOOST_TEST_MESSAGE( "teardown fixture" ); }

    int i;
};

//____________________________________________________________________________//

BOOST_FIXTURE_TEST_SUITE( s, F )

BOOST_AUTO_TEST_CASE( test_case1 )
{
    BOOST_CHECK( i == 1 );
}

//____________________________________________________________________________//

BOOST_AUTO_TEST_CASE( test_case2 )
{
    BOOST_CHECK_EQUAL( i, 0 );
}

//____________________________________________________________________________//

BOOST_AUTO_TEST_SUITE_END()      

那么预期的调用顺序应该是:

setup fixture
test_case1
test_case2
teardown fixture

但实际上是这样的:

setup fixture
test_case1
teardown fixture
setup fixture
test_case2
teardown fixture

我不知道这是否是一个错误,因为通过阅读 BOOST_FIXTURE_TEST_SUITE 文档,我预计正是第一个行为。如果我使用 BOOST_FIXTURE_TEST_CASE,我还可以获得第二种行为。

I'm not quite sure if the accepted answer is correct, because if I use the test code from the boost web site:

#define BOOST_TEST_MODULE example
#include <boost/test/included/unit_test.hpp>

struct F {
    F() : i( 0 ) { BOOST_TEST_MESSAGE( "setup fixture" ); }
    ~F()         { BOOST_TEST_MESSAGE( "teardown fixture" ); }

    int i;
};

//____________________________________________________________________________//

BOOST_FIXTURE_TEST_SUITE( s, F )

BOOST_AUTO_TEST_CASE( test_case1 )
{
    BOOST_CHECK( i == 1 );
}

//____________________________________________________________________________//

BOOST_AUTO_TEST_CASE( test_case2 )
{
    BOOST_CHECK_EQUAL( i, 0 );
}

//____________________________________________________________________________//

BOOST_AUTO_TEST_SUITE_END()      

Then the expected call sequence should be:

setup fixture
test_case1
test_case2
teardown fixture

But in fact it is this:

setup fixture
test_case1
teardown fixture
setup fixture
test_case2
teardown fixture

I don't know if this is a bug, because from reading the BOOST_FIXTURE_TEST_SUITE documentation, I would expect exactly the first behavior. I can also get the second behavior if I use BOOST_FIXTURE_TEST_CASE.

童话 2024-12-28 01:02:00

供将来参考:

这已添加到库中,我相信从 1.36 开始。

For future reference:

This has been added to the library, as of 1.36 I believe.

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