简体   繁体   English

在单独的cpp文件中增强单元测试

[英]Boost unit test in separate cpp files

I want to separate my Boost unit tests into separate .cpp files (eg Test1.cpp, Test2.cpp, Test3.cpp ... etc) So that I do not have 1000 tests in a single cpp file. 我想将我的Boost单元测试分成单独的.cpp文件(例如Test1.cpp,Test2.cpp,Test3.cpp ...等),以便在一个cpp文件中没有1000个测试。 So far I have been getting all kinds of errors when I try to build. 到目前为止,我尝试构建时遇到了各种各样的错误。

Test1.cpp Test1.cpp

#define BOOST_TEST_MODULE MasterTestSuite
#include <boost/test/included/unit_test.hpp>
BOOST_AUTO_TEST_CASE(myTestCase)
{
  BOOST_CHECK(1 == 1);  
}

Test2.cpp 测试2.cpp

#define BOOST_TEST_MODULE MasterTestSuite2
#include <boost/test/included/unit_test.hpp>
BOOST_AUTO_TEST_CASE(myTestCase2)
{
  BOOST_CHECK(2 == 2);  
}

boost-test generates it's own main function when you define BOOST_TEST_MODULE , see: BOOST_TEST_MODULE . 当您定义BOOST_TEST_MODULEboost-test生成其自己的main功能,请参阅: BOOST_TEST_MODULE Some of your errors are likely to be because of this. 您的某些错误很可能是由于这个原因。

I put BOOST_TEST_MODULE in a separate file, eg: 我将BOOST_TEST_MODULE放在单独的文件中,例如:

test_main.cpp test_main.cpp

#ifndef _MSC_VER
#define BOOST_TEST_DYN_LINK
#endif
#define BOOST_TEST_MAIN
#define BOOST_TEST_MODULE Main
#include <boost/test/unit_test.hpp>

And then use test suites to separate unit tests into separate .cpp files, with a test suite in each unit test file eg: 然后使用测试套件将单元测试分成单独的.cpp文件,每个单元测试文件中都有一个test suite ,例如:

Test1.cpp Test1.cpp

#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_SUITE(MyTests)

BOOST_AUTO_TEST_CASE(myTestCase)
{
  BOOST_CHECK(1 == 1);
}

BOOST_AUTO_TEST_SUITE_END()

Test2.cpp 测试2.cpp

#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_SUITE(MyTests2)

BOOST_AUTO_TEST_CASE(myTestCase2)
{
  BOOST_CHECK(2 == 2);
}

BOOST_AUTO_TEST_SUITE_END()

You can find an example of this approach in the tests directory here . 您可以在测试目录这种方法的一个例子在这里

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

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