Boost Test入口/出口夹具,带手动登记
Boost Test entry/exit fixture with manual registration
我使用的是Boost 1.61,我正在使用以下类型的Boost Test设置,我手动注册测试:
// testsuite.cpp
#include <boost/test/unit_test.hpp>
using boost::unit_test::test_suite;
test_suite* init_unit_test_suite(int, char* []) {
test_suite* test = BOOST_TEST_SUITE("TestSuiteName");
test->add(FooTest::suite());
return test;
}
// foo.hpp
#include <boost/test/unit_test.hpp>
using boost::unit_test::test_suite;
class FooTest {
public:
static void testFoo1();
static void testFoo2();
static test_suite* suite();
}
// foo.cpp
#include <foo.hpp>
void FooTest::testFoo1() {
// testFoo1 implementation
}
void FooTest::testFoo2() {
// testFoo2 implementation
}
test_suite* FooTest::suite() {
test_suite* suite = BOOST_TEST_SUITE("FooTest");
suite->add(BOOST_TEST_CASE(&FooTest::testFoo1));
suite->add(BOOST_TEST_CASE(&FooTest::testFoo2));
return suite;
}
我想向FooTest测试套件添加一个入口/出口固定装置,如这里所述。Boost Test文档在自动注册测试的上下文中描述了此功能。
我可以在手动注册测试的设置中使用这个进入/退出固定装置功能吗?如果没有,是否有人建议我如何模仿这种行为,即在进入FooTest测试套件时构建一次对象,该对象可用于测试套件中的所有测试,然后在退出FooTest套件时销毁?
这里也有人问过类似的问题,但我找不到可以使用的答案。
为了防止它对某人有用,通过查看一些Boost Test源文件,我提出了以下似乎符合我的要求的内容,即使用手动注册的入口/出口夹具。
// testsuite.cpp
#include "foo.hpp"
#include <boost/test/unit_test.hpp>
using boost::unit_test::test_suite;
test_suite* init_unit_test_suite(int, char*[]) {
test_suite* test = BOOST_TEST_SUITE("TestSuiteName");
test->add(FooTest::suite());
return test;
}
// bar.hpp (The class to be tested)
class Bar {
public:
Bar(int x, int y) : x_(x), y_(y) {}
int x_;
int y_;
};
// foo.hpp
#include <boost/test/unit_test.hpp>
using boost::unit_test::test_suite;
class FooTest {
public:
static void testFoo1();
static void testFoo2();
static test_suite* suite();
};
// foo.cpp
#include "foo.hpp"
#include "bar.hpp"
#include <boost/make_shared.hpp>
#include <vector>
using boost::unit_test::test_unit_fixture_ptr;
using std::vector;
namespace {
boost::shared_ptr<Bar> bar;
void setup() {
BOOST_TEST_MESSAGE("Creating Bar Instance");
bar = boost::make_shared<Bar>(1, 2);
}
void teardown() {
BOOST_TEST_MESSAGE("Destroying Bar Instance");
bar.reset();
}
}
void FooTest::testFoo1() {
BOOST_TEST_MESSAGE("In testFoo1");
BOOST_CHECK_MESSAGE(bar->x_ == 1, "Checking x_");
}
void FooTest::testFoo2() {
BOOST_TEST_MESSAGE("In testFoo2");
BOOST_CHECK_MESSAGE(bar->y_ == 2, "Checking y_");
}
test_suite* FooTest::suite() {
test_suite* suite = BOOST_TEST_SUITE("FooTest");
// Manually add an entry/exit fixture to the test suite
vector<test_unit_fixture_ptr> fixtures {
boost::make_shared<boost::unit_test::function_based_fixture>(setup, teardown)};
suite->p_fixtures.set(fixtures);
suite->add(BOOST_TEST_CASE(&FooTest::testFoo1));
suite->add(BOOST_TEST_CASE(&FooTest::testFoo2));
return suite;
}
当使用log_level=message
运行时,此代码给出以下输出
Running 2 test cases...
Creating Bar Instance
In testFoo1
In testFoo2
Destroying Bar Instance
*** No errors detected
如果你认为这种方法有缺陷,或者你觉得有更好的方法来实现我想要的,请告诉我。
相关文章:
- 如何使用 Google Test 向测试添加元数据 / 如何将数据从 Google Test 发送到 TestEven
- Boost.TEST with CLion: "Test framework quit unexpectedly"
- 使用INSTANTIATE_TEST_CASE_P的同一夹具的不同实例
- Google Test for OpenCv c++
- 使用 Google Test 对自定义断言函数进行单元测试
- 柯南,CMake.test()生成XML报告
- 如何在 google test in windows 中管理断言
- google test PrintTo for std::set<std::string>
- 我正在尝试学习如何在 c++ 中传递指针,但出现错误:没有用于调用"test"的匹配函数。我做错了什么?
- Xcode Test Navigator 如何在纯C++项目中显示 Google 测试
- 如何期望通过使用Google Test(Mock)以特定频率调用函数
- 无法将 Google Test NuGet 包添加到 Visual Studio Linux C++ 项目中
- 错误:无效使用非静态成员函数"int test::hotplug_callback(libusb_contex
- 为什么会给出"multiple test case"错误?
- 使用 C++ Boost.Test 组织单元测试?
- Caffe在net_.reset上打印整个protext文件(new Net<float>(model_file,TEST))
- Google Test:错误 LNK2019:使用 Visual Studio 2017 与 CMake 进行未解析的外
- C ++ Google test (gtest):如何创建自定义断言和期望?
- 无效使用非静态成员函数 int test::funcAB(int, int)
- Boost Test入口/出口夹具,带手动登记