C ++ unit testing framework: a boost test tutorial -- Part1: boost test crash-Course

Source: Internet
Author: User
ArticleDirectory
    • Runner. cpp
    • Myfootest. cpp (same goes for mybartest. cpp)

Address: http://www.beroux.com/english/articles/boost_unit_testing/

So define C ++ unit testing framework exist, so why boost Test Library? The excellent but outdated Article locking ing the C ++ unit testing framework jungle showed a nice comparison. Since then, the boost Test Library evolved a. Let's see if it improved.

Comparing the C ++ unit test frameworks

After reading the articleGames fromI 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 shoshould 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 shoshould 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 deletests from different files, and fixtures to group the common environment setup and tear-down for a bunch of tests cases. here's how we cocould 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;
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. 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. CppFileBoost_auto_test_caseAnd possiblyBoost_fixture_test_suiteOr similar, and that's it!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.