获得所有boost测试套件/测试用例

编程入门 行业动态 更新时间:2024-10-18 14:24:44
本文介绍了获得所有boost测试套件/测试用例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如标题所示,我想从测试应用程序获取所有测试套件或测试用例(名称),在控制台中或作为xml输出。 测试框架是boost测试库。

As the title says, I want to get all test suites or test cases (name) from a test application, ether in the console or as xml output. Test framework is the boost test library.

有没有选项来实现这个?我在文档中没有发现任何有用的东西。

Is there an option to achieve this? I did not found anything useful in the documentation.

推荐答案

这可以使用全局定位。假设您有一个显式或自动生成的包含 main 的翻译单元(cpp文件),当提供了某个命令行参数时,可以拦截测试执行。然后,您可以使用自定义的访问者遍历测试树,其中列出了所有可用的测试。这是一个小的工作示例,通过编译和链接文件创建测试运行程序 main_test.cpp , a.cpp 和 b.cpp :

This can be done without much intrusion using a global fixture. Assuming you have a translation unit (cpp file) that contains main explicitly or auto generated, you can intercept test execution when a certain command line argument is provided. Then you can traverse the test tree using a customized visitor, which lists all available tests. Here is a small working example, which creates a test runner by compiling and linking files main_test.cpp, a.cpp and b.cpp:

main_test.cpp b

#include <string> #include <iostream> // --- Boost Includes --- #define BOOST_TEST_MODULE MyTestSuite #define BOOST_TEST_DYN_LINK #include <boost/test/unit_test.hpp> using namespace boost::unit_test; struct Visitor : test_tree_visitor { size_t level = 0; void visit( test_case const& test ) { std::string indentation( level, '.' ); std::cout << indentation << test.p_name << std::endl; } bool test_suite_start( test_suite const& suite ) { std::string indentation( level, '.' ); level++; std::cout << indentation << "Suite: " << suite.p_name << std::endl; return true; } void test_suite_finish( test_suite const& suite ) { level--; } }; struct GlobalFixture { GlobalFixture( ) { int argc = framework::master_test_suite( ).argc; for ( int i = 0; i < argc; i++ ) { std::string argument( framework::master_test_suite( ).argv[i] ); if ( argument == "list" ) { Visitor visitor; traverse_test_tree( framework::master_test_suite( ), visitor ); exit( EXIT_SUCCESS ); } } } }; BOOST_GLOBAL_FIXTURE( GlobalFixture )

a.cpp

#include <boost/test/unit_test.hpp> BOOST_AUTO_TEST_SUITE ( TestA ) BOOST_AUTO_TEST_CASE ( TestFoo ) { BOOST_CHECK(true); } BOOST_AUTO_TEST_CASE ( TestBar ) { BOOST_CHECK(true); } BOOST_AUTO_TEST_SUITE_END() // TestA

b.cpp

#include <boost/test/unit_test.hpp> BOOST_AUTO_TEST_SUITE ( TestB ) BOOST_AUTO_TEST_CASE ( TestFoo ) { BOOST_CHECK(true); } BOOST_AUTO_TEST_CASE ( TestBar ) { BOOST_CHECK(true); } BOOST_AUTO_TEST_SUITE_END() // TestA

./somelib_testrunner1 Running 4 test cases... *** No errors detected

传递参数 c $ c>在上面定义的fixture中使用导致

Passing the argument list used in the fixture defined above results in

Suite: MyTestSuite .Suite: TestA ..TestFoo ..TestBar .Suite: TestB ..TestFoo ..TestBar

更多推荐

获得所有boost测试套件/测试用例

本文发布于:2023-11-07 10:51:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1566256.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:测试   套件   boost

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!