用于boost :: test的CppUnit保护器的等价物?

时间:2023-01-13 10:21:58

I've used both CppUnit and boost::test for C++ unittesting. Generally I prefer boost::test, mainly because the auto-test macros minimise the effort to setup tests. But there's one thing I really miss from CppUnit: the ability to register your own "protectors", instances of which automatically wrap all the run tests. (Technically, you install a test "listener", and that can wrap each test in a protector scope).

我已经使用CppUnit和boost :: test进行C ++单元测试。通常我更喜欢boost :: test,主要是因为自动测试宏可以最大限度地减少设置测试的工作量。但是我从CppUnit中确实遗漏了一件事:能够注册自己的“保护者”,其实例会自动包装所有的运行测试。 (从技术上讲,您安装了一个测试“监听器”,并且可以将每个测试包装在一个保护器范围内)。

I've found these invaluable in the past for monitoring unittests for unexpected side effects (e.g checking code hasn't changed the floating-point unit state flags). I can't see any equivalent in the boost::test documentation, although BOOST_FIXTURE_TEST_CASE maybe comes closest.

我发现这些在过去用于监控单元测试以获得意外的副作用是非常宝贵的(例如,检查代码没有改变浮点单元状态标志)。我在boost :: test文档中看不到任何等价物,尽管BOOST_FIXTURE_TEST_CASE可能最接近。

Any suggestions for how to best achieve the same thing as CppUnit's protectors in boost::test ?

有关如何在boost :: test中如何最好地实现与CppUnit的保护器相同的建议吗?

(I haven't really looked into boost::test's implementation yet, but if it's anything like CppUnit it must use something very like protectors itself).

(我还没有真正研究过boost :: test的实现,但如果它像CppUnit一样,它必须使用非常类似于保护器本身的东西)。

1 个解决方案

#1


I've never used CppUnit, so not sure how protectors work. Are you looking for something that wraps individual tests, or the entire test suite?

我从未使用过CppUnit,因此不确定保护器是如何工作的。您正在寻找包含个别测试或整个测试套件的东西吗?

For the former, you could use fixtures as you mention, but as I understand it, fixtures should be considered "outside" the test. They set up whatever the test needs, and cleans it up afterwards. Any actual error-testing should be in the test itself, but can be easily implemented with RAII. Simply define a class which checks whatever you need in its destructor, and then create a local instance of it at the beginning of the test. Since it is constructed first, it gets destructed last, so it can easily check that the test hasn't modified any unexpected state.

对于前者,你可以像你提到的那样使用灯具,但据我所知,灯具应该被认为是“在测试之外”。他们设置了测试所需的一切,然后进行清理。任何实际的错误测试都应该在测试本身,但可以使用RAII轻松实现。只需定义一个类,它在析构函数中检查您需要的任何内容,然后在测试开始时创建它的本地实例。由于它是先构造的,因此最后会被破坏,所以它可以很容易地检查测试是否没有修改任何意外状态。

If you want it to check this after all the tests have executed, you probably want global fixtures

如果你想在执行所有测试后检查它,你可能需要全局装置

#1


I've never used CppUnit, so not sure how protectors work. Are you looking for something that wraps individual tests, or the entire test suite?

我从未使用过CppUnit,因此不确定保护器是如何工作的。您正在寻找包含个别测试或整个测试套件的东西吗?

For the former, you could use fixtures as you mention, but as I understand it, fixtures should be considered "outside" the test. They set up whatever the test needs, and cleans it up afterwards. Any actual error-testing should be in the test itself, but can be easily implemented with RAII. Simply define a class which checks whatever you need in its destructor, and then create a local instance of it at the beginning of the test. Since it is constructed first, it gets destructed last, so it can easily check that the test hasn't modified any unexpected state.

对于前者,你可以像你提到的那样使用灯具,但据我所知,灯具应该被认为是“在测试之外”。他们设置了测试所需的一切,然后进行清理。任何实际的错误测试都应该在测试本身,但可以使用RAII轻松实现。只需定义一个类,它在析构函数中检查您需要的任何内容,然后在测试开始时创建它的本地实例。由于它是先构造的,因此最后会被破坏,所以它可以很容易地检查测试是否没有修改任何意外状态。

If you want it to check this after all the tests have executed, you probably want global fixtures

如果你想在执行所有测试后检查它,你可能需要全局装置