简体   繁体   English

如何定义Boosts测试以在代码中运行?

[英]How to define Boosts tests to run in code?

Using Boost.Test I'm trying to specify the BOOST_TESTS_TO_RUN environment variable in code, so that only some tests will be executed. 使用Boost.Test,我试图在代码中指定BOOST_TESTS_TO_RUN环境变量,以便仅执行一些测试。 Using #define BOOST_TESTS_TO_RUN . 使用#define BOOST_TESTS_TO_RUN

The command line parameter --run_tests works fine. 命令行参数--run_tests可以正常工作。 But I'd like to do it via the environment variable in order to have a single main.cpp in which I can select different suites and modules. 但是我想通过环境变量来实现它,以便拥有一个main.cpp在其中我可以选择不同的套件和模块。

But I'm not sure on how to specify it, or where. 但是我不确定如何指定它,或在哪里。 Should it be before including boost? 应该在包含增强功能之前? What if I have different cpp files with different test suites? 如果我有不同的cpp文件和不同的测试套件怎么办?

module_A.cpp module_A.cpp

#include "SomeHeader.h"
#define BOOST_TESTS_TO_RUN // ??
#include "boost/test/unit_test.hpp"
BOOST_AUTO_TEST_SUITE( moduleA );
BOOST_AUTO_TEST_CASE( test1 ){}
BOOST_AUTO_TEST_CASE( test2 ){}
BOOST_AUTO_TEST_CASE( test3 ){}
BOOST_AUTO_TEST_SUITE_END();

The Boost UTF was not designed that way. Boost UTF不是那样设计的。 If you really want to en-/disable certain tests from your code, you could use manual registration for all your tests and define a master test suite, in which you need to manually register all tests. 如果您确实想从代码中启用/禁用某些测试,则可以对所有测试使用手动注册,并定义一个主测试套件,在该套件中您需要手动注册所有测试。 See the master-test-suite documentation and other pages about manual test registration. 请参阅主测试套件文档和其他有关手动测试注册的页面。 You could then comment-out certain tests you don't want to run. 然后,您可以注释掉某些不想运行的测试。

However, I do not recommend this approach. 但是,我不推荐这种方法。 It's too easy to forget tests and thus harder to maintain. 忘记测试太容易了,因此很难维护。 For debugging you could easily run the test executable by hand and use the command-line parameters. 为了进行调试,您可以轻松地手动运行测试可执行文件并使用命令行参数。

Check instead the new documentation of --run_test that explains that the environment variable BOOST_TEST_RUN_FILTERS can be used for that (also existing in the previous version of boost, but the environment variable usage is less obvious from the doc). 相反,请查看--run_test的新文档,该文档解释了环境变量BOOST_TEST_RUN_FILTERS可以用于此目的(也存在于boost的早期版本中,但是从文档中可以明显看出环境变量的用法)。

Basically, you compile your main.cpp only once with all tests (enabled by default) and then run them selectively from the BOOST_TEST_RUN_FILTERS environment variable. 基本上,您只需对所有测试(默认情况下启用)编译一次main.cpp ,然后从BOOST_TEST_RUN_FILTERS环境变量中有选择地运行它们。

I do not see a problem in doing this, especially wrt. 我看不到这样做的问题,特别是wrt。 the design of boost.test. boost.test的设计。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM