C++ Unit Testing Framework: A Boost Test Tutorial——part1:Boost Test crash-course

 原文地址:http://www.beroux.com/english/articles/boost_unit_testing/

So many C++ unit testing framework exist, so why Boost Test Library? The excellent but outdated article Exploring the C++ Unit Testing Framework Jungle showed a nice comparison. Since then, the Boost Test Library evolved a lot. Let's see if it improved.

Comparing the C++ unit test frameworks

After reading the article of Games from Within I tried those promoted and got disappointed by the date of their last release. Finally I checked if the critics against Boost Test where still valid (4 years later).

Unit tests should be minimal to write, and that was Boost Test weakest point in 2004, but Boost 1.36 has auto-test registration without preprocessing making Boost Test one of the best C++ unit testing framework nowadays.

Introducing Boost Test Library

Here is a sample unit test fully running:


// TODO: Include your class to test here.
#define BOOST_TEST_MODULE MyTest
#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_CASE(MyTestCase)
{
// To simplify this example test, let's suppose we'll test 'float'.
// Some test are stupid, but all should pass.
float x = 9.5f;

BOOST_CHECK(x != 0.0f);
BOOST_CHECK_EQUAL((int)x, 9);
BOOST_CHECK_CLOSE(x, 9.5f, 0.0001f); // Checks differ no more then 0.0001%
}

Using auto-registration and fixtures

Usually you'll probably want to have many tests from different files, and fixtures to group the common environment setup and tear-down for a bunch of tests cases. Here's how we could do that to test two classes "CMyFoo" and "CMyBar".

File structure:

  • Runner.cpp: Defines the main() and how to run the tests.
  • MyFooTest.cpp: Unit tests to test CMyFoo.
  • MyBarTest.cpp: Unit tests to test CMyBar.

Runner.cpp

The runner execute the test cases, its the main(). To customize the output or to run a single test use some command-line arguments. See: Boost.Test > Components > The Unit Test Framework > Components > The Test Log. Example 1: To show more detailed report in XML add command-line arguments: --log_level=all --report_format=XML --report_level=detailed. Example 2: To run all test cases in the suite named "SQLiteDatabaseTest" (with more detailed report), use: --log_level=test_suite --run_test=SQLiteDatabaseTest/*.

#include "StdAfx.h"

#define BOOST_TEST_MODULE "C++ Unit Tests for Foo/Bar"
#include <boost/test/unit_test.hpp>

MyFooTest.cpp (same goes for MyBarTest.cpp)

Here is a fictive example of how to test CMyFoo.

#include "StdAfx.h"
#include "../MyFoo.h"
#include <boost/test/unit_test.hpp>

using namespace std;

struct CMyFooTestFixture
{
CMyFooTestFixture()
: m_configFile("test.tmp")
{
// TODO: Common set-up each test case here.
fclose( fopen(m_configFile.c_str(), "w+") );
}

~CMyFooTestFixture()
{
// TODO: Common tear-down for each test case here.
remove(m_configFile.c_str());
}

// TODO: Possibly put some common tests.
void TestSaveLoad(CMyFoo& foo, bool asBinary)
{
BOOST_CHECK(foo.Save(asBinary));
}

// TODO: Declare some common values accesses in tests here.
string m_configFile;
}

BOOST_FIXTURE_TEST_SUITE(MyFooTest, CMyFooTestFixture);

BOOST_AUTO_TEST_CASE(LoadTestConfigFile)
{
CMyFoo foo;
BOOST_REQUIRE(foo.IsValid()); // Stop here if it fails.
TestSaveLoad(foo, true);
TestSaveLoad(foo, false);
BOOST_CHECK_THROW(foo.Save(nullptr), exception);
}

BOOST_AUTO_TEST_CASE(Name)
{
CMyFoo foo;
foo.SetName(" foooooo ");
BOOST_CHECK_EQUAL(foo.GetName, " foooooo ");
}

BOOST_AUTO_TEST_SUITE_END();

What's the big deal? To test a new class you simply have to: Create a .cpp file with BOOST_AUTO_TEST_CASE and possibly BOOST_FIXTURE_TEST_SUITE or similar, and that's it!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值