BOOST_DATA_TEST_CASE,带夹具支撑
BOOST_DATA_TEST_CASE with fixture support
我正在BOOST_DATA_TEST_CASE
中寻找夹具支持。我为它编写了以下C++宏,但可能有人有更好的东西?
#include <boost/test/unit_test.hpp>
#include <boost/test/data/test_case.hpp>
#define BOOST_FIXTURE_DATA_TEST_CASE_IMPL( arity, test_name, F, dataset, params )
struct test_name : public F {
template<BOOST_PP_ENUM_PARAMS(arity, typename Arg)>
static void test_method( BOOST_DATA_TEST_CASE_PARAMS( params ) )
{
BOOST_TEST_CHECKPOINT('"' << #test_name << "" fixture entry.");
test_name t;
BOOST_TEST_CHECKPOINT('"' << #test_name << "" entry.");
BOOST_TEST_CONTEXT( ""
BOOST_PP_SEQ_FOR_EACH(BOOST_DATA_TEST_CONTEXT, _, params))
t._impl(BOOST_PP_SEQ_ENUM(params));
BOOST_TEST_CHECKPOINT('"' << #test_name << "" exit.");
}
private:
template<BOOST_PP_ENUM_PARAMS(arity, typename Arg)>
void _impl(BOOST_DATA_TEST_CASE_PARAMS( params ));
};
BOOST_AUTO_TU_REGISTRAR( test_name )(
boost::unit_test::data::ds_detail::make_test_case_gen<test_name>(
BOOST_STRINGIZE( test_name ),
__FILE__, __LINE__,
boost::unit_test::data::make(dataset) ),
boost::unit_test::decorator::collector::instance() );
template<BOOST_PP_ENUM_PARAMS(arity, typename Arg)>
void test_name::_impl( BOOST_DATA_TEST_CASE_PARAMS( params ) )
/**/
#define BOOST_AUTO_DATA_TEST_CASE_WITH_PARAMS( test_name, dataset, ... )
BOOST_FIXTURE_DATA_TEST_CASE_IMPL( BOOST_PP_VARIADIC_SIZE(__VA_ARGS__),
test_name, BOOST_AUTO_TEST_CASE_FIXTURE, dataset,
BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__) )
/**/
#define BOOST_AUTO_DATA_TEST_CASE_NO_PARAMS( test_name, dataset )
BOOST_AUTO_DATA_TEST_CASE_WITH_PARAMS( test_name, dataset, sample )
/**/
#if BOOST_PP_VARIADICS_MSVC
#define BOOST_AUTO_DATA_TEST_CASE( ... )
BOOST_PP_CAT(
BOOST_PP_IIF(BOOST_PP_EQUAL(BOOST_PP_VARIADIC_SIZE(__VA_ARGS__),2),
BOOST_AUTO_DATA_TEST_CASE_NO_PARAMS,
BOOST_AUTO_DATA_TEST_CASE_WITH_PARAMS) (__VA_ARGS__), )
/**/
#else
#define BOOST_AUTO_DATA_TEST_CASE( ... )
BOOST_PP_IIF(BOOST_PP_EQUAL(BOOST_PP_VARIADIC_SIZE(__VA_ARGS__),2),
BOOST_AUTO_DATA_TEST_CASE_NO_PARAMS,
BOOST_AUTO_DATA_TEST_CASE_WITH_PARAMS) (__VA_ARGS__)
/**/
#endif
BOOST_DATA_TEST_CASE
用法示例:
#include <boost/range/iterator_range.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/filesystem/operations.hpp>
struct fixure
{
fixure() :
_Mydir(boost::filesystem::temp_directory_path() / boost::filesystem::unique_path("test-%%%%-%%%%-%%%%-%%%%"))
{
create_directory(_Mydir);
}
~fixure()
{
_TRY_BEGIN
remove_all(_Mydir);
_CATCH_ALL
_CATCH_END
}
boost::filesystem::path const _Mydir;
};
BOOST_FIXTURE_TEST_SUITE(suite, fixure)
namespace {
std::array<char const *, 4> const _Ourdata = { "A", "B", "C", "D" };
}
BOOST_AUTO_DATA_TEST_CASE(test, _Ourdata, _Str)
{
BOOST_REQUIRE(_Str);
BOOST_REQUIRE(is_directory(_Mydir));
auto const _File = _Mydir / _Str;
BOOST_TEST_MESSAGE(_File);
std::fstream _Stream;
_Stream.open(_File.c_str(), std::ios::out);
_Stream << _Str;
_Stream.close();
BOOST_REQUIRE(is_regular_file(_File));
}
BOOST_AUTO_TEST_SUITE_END()
test_suite
模式输出:
Running 4 test cases...
Entering test module "example"
Entering test suite "suite"
Entering test case "test"
"C:UsersXXXAppDataLocalTemptest-4445-a983-8ba6-09efA"
Failure occurred in a following context:
_Str = A;
Leaving test case "test"; testing time: 17ms
Entering test case "test"
"C:UsersXXXAppDataLocalTemptest-d4c4-c5f6-6154-7200B"
Failure occurred in a following context:
_Str = B;
Leaving test case "test"; testing time: 10ms
Entering test case "test"
"C:UsersXXXAppDataLocalTemptest-9f96-4f2c-b132-c541C"
Failure occurred in a following context:
_Str = C;
Leaving test case "test"; testing time: 9ms
Entering test case "test"
"C:UsersXXXAppDataLocalTemptest-f0cf-962c-aed3-1cf8D"
Failure occurred in a following context:
_Str = D;
Leaving test case "test"; testing time: 10ms
Leaving test suite "suite"; testing time: 61ms
Leaving test module "example"; testing time: 76ms
*** No errors detected
更新:使用
std::array<>
从Boost 1.62开始,您可以使用Boost_DATA_TEST_CASE_F进行以下操作:
使用特定的数据集和fixture声明和注册数据驱动的测试用例。
BOOST_DATA_TEST_CASE_F(my_fixture, test_case_name, dataset, var1, var2..., varN)
参考:
https://www.boost.org/doc/libs/1_62_0/libs/test/doc/html/boost_test/utf_reference/test_org_reference/test_org_boost_test_dataset_fixture.html
相关文章:
- 使用INSTANTIATE_TEST_CASE_P的同一夹具的不同实例
- 在 C++ 的 Switch Case 中创建对象后对对象调用方法
- switch-case语句如何在Arduino/C++中工作
- 为什么会给出"multiple test case"错误?
- 谷歌测试:如何为多个测试只运行一次夹具?
- 为什么我总是"error: expected unqualified-id before 'case'"?
- 一个'case'在简单的'switch'语句中不起作用
- 从谷歌测试中的派生夹具派生夹具
- Use case for std::promise::set_{value|exception}_at_thread_e
- GMock 测试夹具在 Windows 上崩溃
- 如何在Fitnesse测试中生成夹具的文档
- 如何在C++中将字符串替换为"Match case"和"Match whole word"
- Boost单元测试夹具继承测试类,是否可以访问受保护的方法
- C++谷歌测试在哪里放置测试夹具构造函数定义
- 使用夹具时的升压单元测试过滤器
- 为什么您可以在 switch 语句中的"默认"下定义变量,而不能在"case"下定义变量
- 当"if else"已经存在时,我为什么要学习"switch case"
- 测试夹具中的设置与构造函数
- 使用枚举关闭switch/case语句的编译器警告
- 为什么case语句中的标签应该是常量