C++ユニットテスト:boost.test
C++ユニットテスト:boost.test
1)準備
1.1)boost testのコンパイル
リファレンス b2 Invocation Getting Started on Windows Getting Started on Unix Variants
中国語では、この2編を参考にしてください. Boostダウンロードインストールコンパイル構成マニュアル VS 2010コンパイルインストールboostライブラリ リンク C++ Compilers: Microsoft Visual C++ linkとruntime-link、sharedとstatic を組み合わせます
1.2)boost testの導入
2)使用
公式ドキュメントBoost Test Libraryは詳しく説明しています.主にいくつかの側面があります.
2.1) Hello the testing world
test_hello.cc
公式の例は簡単です「BOOST_TEST_MODULE」、テストモジュールを定義します.その後include「unit_test.hpp」を行います. 「BOOST_AUTO_TEST_CASE」、自動登録試験用例. テストツールは「WARN」、「CHECK」、「REQUIRE」の3つのレベルに分けられます.「CHECK」と「REQUIRE」の違いは、前者は失敗しても継続します.後者は必須と判断し、重大なエラーのため、現在のテストを直接終了します. テストツールの参考はtesting tools referenceを参照してください.
2.2)自動登録テストキット(Test suites)
以下のマクロで「TEST_CASE」を包むと、
test_suite.ccは、上述の2つの「TEST_SUITE」を定義している.test_case 1、test_case 1はtest_suite 1内、test_case 3、test_case 4はtest_suite 2内である. 注意test_case_on_file_scope、その上下はそれぞれtest_suite 2のtest_case3、test_case4.つまり、テストキットは、異なるファイルに配置するために複数の部分に分けることができます.
2.3)テスト治具使用(Test fixtures)
繰り返しのテストデータは、1つの治具に入れることができます.テンプレートは次のとおりです.
次に、次の操作を行います.「BOOST_FIXTURE_TEST_CASE」は、試験例に用いられる. 「BOOST_FIXTURE_TEST_SUITE」は、テストキットに使用されます.これで、テストキットの各テスト例は書かなくてもいいです. 「BOOST_GLOBAL_FIXTURE」は、グローバルに使用されます.
test_suite.cc出力には、4つのセットの「setup fixture」と「teardown fixture」があり、各使用例の下にそれぞれ1部ずつ使用されます. "global setup"と"global teardown"はそれぞれヘッダの最後にあり、1つのインスタンスしかありません.公式ドキュメントにはメンバーへのアクセス方法が表示されず、ここでは単一の例で処理されています. 3)まとめ
本文は主にboostユニットテストフレームワークの使用を紹介した.
附1:サンプルエンジニアリングbtest_start
ダウンロード:btest_start.zip.
ディレクトリツリーは次のとおりです.
C::B工事、boost_1_55_0/ディレクトリWindowsでの設定に使用しました.
1)準備
1.1)boost testのコンパイル
cd boost_1_55_0
# On Windows
bootstrap.bat
b2 --toolset=msvc-10.0 --build-type=minimal --with-test runtime-link=static stage
# 1) --toolset=msvc-10.0: , VS2010。 1。
# 2) --build-type=minimal: , :
# link=static runtime-link=shared threading=multi variant=debug,release
# 3) --with-test: test。 '--with', 。
# 4) runtime-link=static: 。link runtime-link , 2。
# 5) stage: , 。
# On Linux
#
ll /usr/lib/libboost*
# yum list installed boost*
./bootstrap.sh
./b2 --toolset=gcc --build-type=minimal --with-test stage
# 1) --prefix: , /usr/local。
# 2) --build-type=minimal: ,Linux :
# link=static,shared threading=multi variant=release
#
./b2 --help
./b2 --show-libraries
リファレンス
中国語では、この2編を参考にしてください.
1.2)boost testの導入
# On Windows,
"IncludePath" += boost_1_55_0
"LibraryPath" += boost_1_55_0\stage\lib
:/EHa,/MT[d]
# On Linux,
:BOOST_TEST_DYN_LINK
:-lboost_unit_test_framework
2)使用
公式ドキュメントBoost Test Libraryは詳しく説明しています.主にいくつかの側面があります.
2.1) Hello the testing world
test_hello.cc
#define BOOST_TEST_MODULE MyTest
#include <boost/test/unit_test.hpp>
int add( int i, int j ) { return i+j; }
BOOST_AUTO_TEST_CASE( my_test )
{
// seven ways to detect and report the same error:
BOOST_CHECK( add( 2,2 ) == 4 ); // #1 continues on error
BOOST_REQUIRE( add( 2,2 ) == 4 ); // #2 throws on error
if( add( 2,2 ) != 4 )
BOOST_ERROR( "Ouch..." ); // #3 continues on error
if( add( 2,2 ) != 4 )
BOOST_FAIL( "Ouch..." ); // #4 throws on error
if( add( 2,2 ) != 4 ) throw "Ouch..."; // #5 throws on error
BOOST_CHECK_MESSAGE( add( 2,2 ) == 4, // #6 continues on error
"add(..) result: " << add( 2,2 ) );
BOOST_CHECK_EQUAL( add( 2,2 ), 4 ); // #7 continues on error
}
公式の例は簡単です
2.2)自動登録テストキット(Test suites)
以下のマクロで「TEST_CASE」を包むと、
BOOST_AUTO_TEST_SUITE(test_suite_name)
BOOST_AUTO_TEST_SUITE_END()
test_suite.cc
#define BOOST_TEST_MODULE MySuiteTest
#include <boost/test/included/unit_test.hpp>
/* test_suite1 start */
BOOST_AUTO_TEST_SUITE( test_suite1 )
// test_case1 in test_suite1
BOOST_AUTO_TEST_CASE( test_case1 )
{
BOOST_WARN( sizeof(int) < 4 );
}
// test_case2 in test_suite1
BOOST_AUTO_TEST_CASE( test_case2 )
{
BOOST_REQUIRE_EQUAL( 1, 2 ); // note: REQUIRE
BOOST_FAIL( "Should never reach this line" );
}
BOOST_AUTO_TEST_SUITE_END()
/* test_suite1 end */
/* test_suite2 start */
BOOST_AUTO_TEST_SUITE( test_suite2 )
// test_case3 in test_suite2
BOOST_AUTO_TEST_CASE( test_case3 )
{
BOOST_CHECK( true );
}
BOOST_AUTO_TEST_SUITE_END()
/* test_suite2 end */
// test_case_on_file_scope
BOOST_AUTO_TEST_CASE( test_case_on_file_scope )
{
BOOST_CHECK( true );
}
/* test_suite2 start */
BOOST_AUTO_TEST_SUITE( test_suite2 )
// test_case4 in test_suite2
BOOST_AUTO_TEST_CASE( test_case4 )
{
BOOST_CHECK( false );
}
BOOST_AUTO_TEST_SUITE_END()
/* test_suite2 end */
2.3)テスト治具使用(Test fixtures)
繰り返しのテストデータは、1つの治具に入れることができます.テンプレートは次のとおりです.
struct <fixture-name>{
<fixture-name>(); // setup function
~<fixture-name>(); // teardown function
};
次に、次の操作を行います.
test_suite.cc
#define BOOST_TEST_MODULE MyFixtureTest
#include <boost/test/included/unit_test.hpp>
#include <iostream>
/* 3) Global fixture */
struct MyConfig {
MyConfig() : g_i( 0 )
{
instance() = this;
std::cout << "global setup
";
}
~MyConfig()
{
std::cout << "g_i: " << g_i << std::endl;
std::cout << "global teardown
";
}
static MyConfig *&instance();
int g_i;
};
BOOST_GLOBAL_FIXTURE(MyConfig);
MyConfig *&MyConfig::instance()
{
static MyConfig *s_inst = 0;
return s_inst;
}
/* 1) Per test case fixture */
struct F {
F() : i( 0 ) { std::cout << "setup fixture
"; }
~F() { std::cout << "teardown fixture
"; }
int i;
};
BOOST_FIXTURE_TEST_CASE( test_case1, F )
{
BOOST_CHECK( i == 1 ); // failed: i == 0
++i;
}
BOOST_FIXTURE_TEST_CASE( test_case2, F )
{
BOOST_CHECK_EQUAL( i, 0 ); // pass: i == 0
++(MyConfig::instance()->g_i);
}
BOOST_AUTO_TEST_CASE( test_case3 )
{
BOOST_CHECK( true );
std::cout << "g_i: " << MyConfig::instance()->g_i << std::endl;
}
/* 2) Test suite level fixture */
BOOST_FIXTURE_TEST_SUITE( test_suite1, F )
BOOST_AUTO_TEST_CASE( test_case1 )
{
BOOST_CHECK( i == 1 ); // failed: i == 0
++i;
}
BOOST_AUTO_TEST_CASE( test_case2 )
{
BOOST_CHECK_EQUAL( i, 0 ); // pass: i == 0
++(MyConfig::instance()->g_i);
}
BOOST_AUTO_TEST_SUITE_END()
本文は主にboostユニットテストフレームワークの使用を紹介した.
附1:サンプルエンジニアリングbtest_start
ダウンロード:btest_start.zip.
ディレクトリツリーは次のとおりです.
btest_start/
├─build/
│ ├─btest_start-gcc.cbp # for gnu gcc
│ └─btest_start-msvc.cbp # for msvc 2010
├─src/
├─third_party/
│ └─boost_1_55_0/
└─tools/
C::B工事、boost_1_55_0/ディレクトリWindowsでの設定に使用しました.