在单独的cpp文件中增强单元测试 [英] Boost unit test in separate cpp files

查看:53
本文介绍了在单独的cpp文件中增强单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将我的Boost单元测试分成单独的.cpp文件(例如Test1.cpp,Test2.cpp,Test3.cpp等),以便在一个cpp文件中没有1000个测试.到目前为止,当我尝试构建时,我遇到了各种各样的错误.

I want to separate my Boost unit tests into separate .cpp files (e.g. Test1.cpp, Test2.cpp, Test3.cpp ... etc) So that I do not have 1000 tests in a single cpp file. 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

Test2.cpp

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

推荐答案

boost-test 在定义 BOOST_TEST_MODULE 时生成其自己的 main 函数,请参阅: BOOST_TEST_MODULE.您的某些错误很可能是由于这个原因.

boost-test generates it's own main function when you define BOOST_TEST_MODULE, see: BOOST_TEST_MODULE. Some of your errors are likely to be because of this.

我将 BOOST_TEST_MODULE 放在单独的文件中,例如:

I put BOOST_TEST_MODULE in a separate file, e.g.:

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 e.g.:

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

Test2.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.

这篇关于在单独的cpp文件中增强单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆