整装使用Boost.Test入门 [英] Helping getting started using Boost.Test

查看:114
本文介绍了整装使用Boost.Test入门的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图启动单元测试。我在看一些C ++框架,并想尝试Boost.Test。文档似乎很透彻,这是一个有点势不可挡,尤其是一个新来的单元测试。因此,这里是我想要一个情况:

I am trying to start unit testing. I am looking at a few C++ frameworks and want to try Boost.Test. The documentation seems very thorough, and it's a bit overwhelming, especially someone new to unit testing. So here's a situation that I want:

比方说,我有2个班,酒吧。我想在不同的pferably写一套测试酒吧 A的测试套件,$ P $文件。我只希望,如果我用命令行参数运行程序来运行测试。所以,我的的main()应该是这个样子:

Let's say I have 2 classes, Foo and Bar. I want to write a suite of tests for Foo and a suite of tests for Bar, preferably in different files. I want to run the tests only if I run the program with a command line parameter. So my main() should look something like:

int main(int argc, const char* argv[])
{
    if (argc == 1 && strcmp(argv[0], "-test") == 0)
        run_all_tests();
    else
        return program_main(argc, argv);
}

我觉得 test_foo.cpp 应该是这样的:

#include "foo.hpp"
#define BOOST_TEST_MODULE Foo test
#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_SUITE( Foo_Test )

BOOST_AUTO_TEST_CASE( Foo1 )
{
    Foo f;
    BOOST_CHECK( f.isValid() );
}

BOOST_AUTO_TEST_CASE( Foo2 )
{
    Foo f;
    BOOST_CHECK( f.baz() == 5 );
}

BOOST_AUTO_TEST_SUITE_END()

不过,我不知道(1)什么实际的命令来运行测试的,和(2)如何真正告诉我要运行每一个测试库。

However, I don't know (1) what the actual command to run the tests is, and (2) how to actually tell the library that I want to run EVERY test.

因此​​,谁拥有Boost.Test经验吗?在详细地有人能帮忙吗?谢谢你这么多。

So, who has experience with Boost.Test? Can someone help in a detailed way? Thank you so much.

推荐答案

BOOST.Test非常灵活,你也许可以做你想做的。不过既然你说你是新来的单元测试,你应该遵循标准的单元测试结构。

BOOST.Test is very flexible and you can probably do what you want. However since you say you are new to unit testing, you should probably follow the standard unit testing structure.

这是对每一个项目你是单元测试一个单独的测试项目。然后包括你需要建立测试项目的源代码和库。

This is to have a separate test project for each project you are unit testing. Then include the sources and libraries you need to build the test project.

这是由于清洁剂中有你的主要项目中没有测试逻辑可能会得到意外地运行,这是容易,因为他们有自己的可执行文件来运行测试。这种方法也适用于检测库。如果按照这个结构,你会发现,大部分的BOOST.Test默认的工作开箱即用,你可以只是担心你编写的测试和code。

This is cleaner as there are no test logic in your main project that might get run accidentally and it is easy to run the tests as they have their own executable. This approach also works for testing libraries. If you follow this structure you will find that most of the BOOST.Test defaults work out of the box and you can just worry about writing you tests and code.

这篇关于整装使用Boost.Test入门的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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