zoukankan      html  css  js  c++  java
  • C++ Unit Testing Framework: A Boost Test Tutorial——part1:Boost Test crashcourse

     原文地址: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!

  • 相关阅读:
    2010年度最有技术含量攻击:Padding Oracle Attack 狼人:
    微软宣布最新企业安全产品FEP 免费试用 狼人:
    金山卫士开放第三批源代码 ARP防火墙可下载 狼人:
    Gawker攻击事件暴露密码保护缺陷 狼人:
    Win7和Mac及HTML5将成黑客2011年攻击重点 狼人:
    开发人员发现Chrome浏览器漏洞获谷歌重奖 狼人:
    FBI被指在OpenBSD的IPSEC协议栈中放置后门 狼人:
    微软发布重大安全忧患警告或影响全球9亿用户 狼人:
    MHTML中曝出0day漏洞 影响各版Windows 狼人:
    nullnullCSharp Tools Compiler
  • 原文地址:https://www.cnblogs.com/strinkbug/p/1359381.html
Copyright © 2011-2022 走看看