C++ Boost.Test:寻找一个有效的非平凡测试套件示例/教程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2906095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Boost.Test: Looking for a working non-Trivial Test Suite Example / Tutorial
提问by Robert S. Barnes
The Boost.Test documentationand examples don't really seem to contain any non-trivial examples and so far the two tutorials I've found hereand herewhile helpful are both fairly basic.
在如Boost.Test文档和例子并不真的似乎包含我发现任何不平凡的例子,到目前为止,两个教程这里和这里同时有帮助都是非常基本的。
I would like to have a master test suite for the entire project, while maintaining per module suites of unit tests and fixtures that can be run independently. I'll also be using a mock server to test various networking edge cases.
我希望有一个用于整个项目的主测试套件,同时维护可以独立运行的单元测试和装置的每个模块套件。我还将使用模拟服务器来测试各种网络边缘情况。
I'm on Ubuntu 8.04, but I'll take any example Linux or Windows since I'm writing my own makefiles anyways.
我使用的是 Ubuntu 8.04,但我会以任何 Linux 或 Windows 为例,因为我正在编写自己的 makefile。
Edit
编辑
As a test I did the following:
作为测试,我执行了以下操作:
// test1.cpp
#define BOOST_TEST_MODULE Regression
#include <boost/test/included/unit_test.hpp>
BOOST_AUTO_TEST_SUITE(test1_suite)
BOOST_AUTO_TEST_CASE(Test1)
{
BOOST_CHECK(2 < 1);
}
BOOST_AUTO_TEST_SUITE_END()
// test2.cpp
#include <boost/test/included/unit_test.hpp>
BOOST_AUTO_TEST_SUITE(test2_suite)
BOOST_AUTO_TEST_CASE(Test1)
{
BOOST_CHECK(1<2);
}
BOOST_AUTO_TEST_SUITE_END()
Then I compile it: g++ test1.cpp test2.cpp -o tests
然后我编译它: g++ test1.cpp test2.cpp -o tests
This gives me about a bazillion "multiple definition of" errors during linking.
这给了我关于链接过程中大量“多重定义”错误的信息。
When it's all in a single file it works fine.
当它全部在一个文件中时,它工作正常。
回答by Robert S. Barnes
C++ Unit Testing With Boost.Test
(permanent link: http://web.archive.org/web/20160524135412/http://www.alittlemadness.com/2009/03/31/c-unit-testing-with-boosttest/)
(永久链接:http: //web.archive.org/web/20160524135412/http: //www.alittlemadness.com/2009/03/31/c-unit-testing-with-boosttest/)
The above is a brilliant article and better than the actual Boost documentation.
以上是一篇精彩的文章,比实际的 Boost 文档更好。
Edit:
编辑:
I also wrote a Perl script which will auto-generate the makefile and project skeleton from a list of class names, including both the "all-in-one" test suite and a stand alone test suite for each class. It's called makeSimpleand can be downloaded from Sourceforge.net.
我还编写了一个 Perl 脚本,它将从类名列表中自动生成 makefile 和项目框架,包括“一体机”测试套件和每个类的独立测试套件。它叫做 makeSimple,可以从 Sourceforge.net 下载。
What I found to be the basic problem is that if you want to split your tests into multiple files you have to link against the pre-compiled test runtime and not use the "headers only" version of Boost.Test. You have to add #define BOOST_TEST_DYN_LINK
to each file and when including the Boost headers for example use <boost/test/unit_test.hpp>
instead of <boost/test/included/unit_test.hpp>
.
我发现的基本问题是,如果您想将测试拆分为多个文件,您必须链接到预编译的测试运行时,而不是使用 Boost.Test 的“仅标头”版本。您必须添加#define BOOST_TEST_DYN_LINK
到每个文件中,并且在包含 Boost 标头时,例如使用<boost/test/unit_test.hpp>
而不是<boost/test/included/unit_test.hpp>
.
So to compile as a single test:
因此,要编译为单个测试:
g++ test_main.cpp test1.cpp test2.cpp -lboost_unit_test_framework -o tests
or to compile an individual test:
或者编译一个单独的测试:
g++ test1.cpp -DSTAND_ALONE -lboost_unit_test_framework -o test1
.
.
// test_main.cpp
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE Main
#include <boost/test/unit_test.hpp>
// test1.cpp
#define BOOST_TEST_DYN_LINK
#ifdef STAND_ALONE
# define BOOST_TEST_MODULE Main
#endif
#include <boost/test/unit_test.hpp>
BOOST_AUTO_TEST_SUITE(test1_suite)
BOOST_AUTO_TEST_CASE(Test1)
{
BOOST_CHECK(2<1);
}
BOOST_AUTO_TEST_SUITE_END()
// test2.cpp
#define BOOST_TEST_DYN_LINK
#ifdef STAND_ALONE
# define BOOST_TEST_MODULE Main
#endif
#include <boost/test/unit_test.hpp>
BOOST_AUTO_TEST_SUITE(test2_suite)
BOOST_AUTO_TEST_CASE(Test1)
{
BOOST_CHECK(1<2);
}
BOOST_AUTO_TEST_SUITE_END()
回答by Edward Strange
I don't know what else you really need than what's in the later tutorial. I've done everything I need to in just that way. Not sure I understand your description either.
除了后面的教程中的内容,我不知道您真正需要什么。我已经以这种方式完成了我需要做的一切。不确定我是否理解你的描述。
One thing that you might be asking for is the ability to have more than one .cpp file in your test program. That's as simple as only defining BOOST_TEST_MODULE in one of those .cpp files. We have a "driver.cpp" file in all our test programs that just defines that and includes the unit test header. All the rest of the .cpp files (scoped by module or concept) only include the unit test header, they do not define that variable.
您可能要求的一件事是能够在您的测试程序中拥有多个 .cpp 文件。这就像只在那些 .cpp 文件之一中定义 BOOST_TEST_MODULE 一样简单。我们在所有测试程序中都有一个“driver.cpp”文件,该文件仅定义了该文件并包含单元测试标头。所有其余的 .cpp 文件(由模块或概念限定)仅包含单元测试头,它们不定义该变量。
If you want to both be able to compile them together and compile them separately then you could use your own -D variable to define BOOST_TEST_MODULE or not.
如果您希望两者都能够一起编译并分别编译它们,那么您可以使用自己的 -D 变量来定义 BOOST_TEST_MODULE 或不。
If you're looking for a way to run a bunch of test programs in one single run and get a report then you could look at the automake method of doing tests or, better yet, the CMake method (CTest). Pretty sure you can use CTest from your own makefile if you insist.
如果您正在寻找一种在一次运行中运行一堆测试程序并获得报告的方法,那么您可以查看进行测试的 automake 方法,或者更好的是,CMake 方法 (CTest)。如果您坚持,可以肯定您可以从您自己的 makefile 中使用 CTest。
回答by Hindu
When I only start with Boost.Test the following article with source code was very helpful: Boost test setup and usage
当我只开始使用 Boost.Test 时,以下带有源代码的文章非常有帮助: Boost test setup and usage