122
12/02/11 Boost.勉強会 #8 大阪 1 Boost.Testの紹介 @hotwatermorning

Introduction to boost test

Embed Size (px)

DESCRIPTION

Boost.勉強会#8大阪にて発表したBoost.Testの紹介

Citation preview

Page 1: Introduction to boost test

12/02/11 Boost.勉強会 #8 大阪 1

Boost.Testの紹介

@hotwatermorning

Page 2: Introduction to boost test

Boost.勉強会 #8 大阪 212/02/11

自己紹介

● @hotwatermorning● 札幌から来ました

Page 3: Introduction to boost test

Boost.勉強会 #8 大阪 312/02/11

自己紹介

● @hotwatermorning● 札幌から来ました● 今季は水道が2回凍結しました。

Page 4: Introduction to boost test

Boost.勉強会 #8 大阪 412/02/11

自己紹介

● 2011年は、

<= C++の同人誌書いたり、

Boost.勉強会 #6 @札幌を開催したりしました =>

http://www.flickr.com/photos/miio119/6318681082/

Page 5: Introduction to boost test

12/02/11 Boost.勉強会 #8 大阪 5

Introduction

Page 6: Introduction to boost test

6Boost.勉強会 #8 大阪12/02/11

IntroductionIntroduction

What's

Features

How to use

● 今日のお話● Boost.Testの紹介● Boost.TestというBoostのユニットテストフレームワークを紹介します

Conclusion

Page 7: Introduction to boost test

7Boost.勉強会 #8 大阪12/02/11

IntroductionIntroduction

What's

Features

How to use

● 主な対象者● C++でのテストに興味がある人。● Boost.Testに興味がある人。● Boost.TestをDISりたい人。

Conclusion

Page 8: Introduction to boost test

8Boost.勉強会 #8 大阪12/02/11

IntroductionIntroduction

What's

Features

How to use

〜 本日のレシピ 〜● What's the Boost.Test?

● Boost.Testとは?● What are the Features?

● どんな機能がある?● How to use the Boost.Test

● Boost.Testの使い方/書き方

Conclusion

Page 9: Introduction to boost test

12/02/11 Boost.勉強会 #8 大阪 9

What's the Boost.Test?

Page 10: Introduction to boost test

10Boost.勉強会 #8 大阪12/02/11

What's the Boost.Test?

● Boost.Testとは● C++で書かれたC++のテストライブラリ

● Boost C++ Librariesに含まれている● ドキュメントが長い

Introduction

What's

Features

How to use

Conclusion

Page 11: Introduction to boost test

11Boost.勉強会 #8 大阪12/02/11

What's the Boost.Test?

● Boost.Testとは● Execution Monitor● Program Execution Monitor● Minimal Testing Facility● Unit Test Framework

の4つの機能

Introduction

What's

Features

How to use

Conclusion

Page 12: Introduction to boost test

12Boost.勉強会 #8 大阪12/02/11

What's the Boost.Test?

● Boost.Testとは● Execution Monitor● Program Execution Monitor● Minimal Testing Facility● Unit Test Framework

の4つの機能

Introduction

What's

Features

How to use

Conclusion

Page 13: Introduction to boost test

13Boost.勉強会 #8 大阪12/02/11

What's the Boost.Test?

● Execution Monitor● Boost.Testの基礎● 関数の実行を監視して統一的なエラーレポーティングの機能を提供する。● 例外● UNIXのシグナル● WindowsのSEH

● 面倒なエラー検出を簡単にする。

Introduction

What's

Features

How to use

Conclusion

Page 14: Introduction to boost test

14Boost.勉強会 #8 大阪12/02/11

What's the Boost.Test?

● Execution Monitor● Boost.Testの基礎● 関数の実行を監視して統一的なエラーレポーティングの機能を提供する。● 例外● UNIXのシグナル● WindowsのSEH

● 面倒なエラー検出を簡単にする。

Introduction

What's

Features

How to use

Conclusion

Page 15: Introduction to boost test

15Boost.勉強会 #8 大阪12/02/11

What's the Boost.Test?

● Execution Monitor● Boost.Testの基礎● 受け取れる例外は3種類。

● C文字列● std::string● std::exceptionから派生したクラス

Introduction

What's

Features

How to use

Conclusion

Page 16: Introduction to boost test

Boost.勉強会 #8 大阪 1612/02/11

What's the Boost.Test?

#include <stdexcept>

#include <iostream>

#include <boost/test/execution_monitor.hpp>

#include <boost/test/utils/basic_cstring/io.hpp>

int foo() {

throw std::runtime_error("some error has occured");

return 0;

}

int main() {

boost::execution_monitor mon;

try {

return mon.execute(foo);

ᅠ ᅠ ᅠ } catch(boost::execution_exception const &e) {ᅠ ᅠ ᅠ ᅠ std::cout << e.what() << std::endl;ᅠ ᅠ }}

Page 17: Introduction to boost test

17Boost.勉強会 #8 大阪12/02/11

What's the Boost.Test?

● Execution Monitor● Boost.Testの基礎● 受け取れる例外は、

● C文字列● std::string● std::exceptionから派生したクラスの3種類。

● これ以外の例外をExecution Monitorで捕捉したい場合はexecution_monitorのregister_exception_translatorを使用する。

Introduction

What's

Features

How to use

Conclusion

Page 18: Introduction to boost test

Boost.勉強会 #8 大阪 1812/02/11

What's the Boost.Test?

struct my_error {

my_error(int code) : code_(code) {}

int code_;

};

void trns_my_error(my_error const& e) {

throw std::domain_error(e.code_ == 0 ? "code is zero" : "otherwise");

}

int main() {

boost::execution_monitor mon;

mon.register_exception_translator<my_error>(&trns_my_error);

try {

return mon.execute(bar); //bar may throw my_error.

} catch(boost::execution_exception const &e) {

std::cout << e.what() << std::endl;

}

}

Page 19: Introduction to boost test

19Boost.勉強会 #8 大阪12/02/11

What's the Boost.Test?

● Boost.Testとは● Execution Monitor● Program Execution Monitor● Minimal Testing Facility● Unit Test Framework

の4つの機能

Introduction

What's

Features

How to use

Conclusion

Page 20: Introduction to boost test

20Boost.勉強会 #8 大阪12/02/11

What's the Boost.Test?

● Program Execution Monitor● Execution Monitorで監視された環境でプログラムを実行することで、エラーレポーティングを簡単にする。

● main関数の代わりに提供されるcpp_main関数からプログラムを開始する。

Introduction

What's

Features

How to use

Conclusion

Page 21: Introduction to boost test

21Boost.勉強会 #8 大阪12/02/11

What's the Boost.Test?

● Program Execution Monitor● cpp_main関数内から例外がなげられたり、0以外の値をcpp_mainから返すとエラーだとみなす。

Introduction

What's

Features

How to use

Conclusion

Page 22: Introduction to boost test

Boost.勉強会 #8 大阪 2212/02/11

What's the Boost.Test?

#include <iostream>#include <boost/test/included/prg_exec_monitor.hpp>

int cpp_main(int, char* []){ std::cout << "Hello, world\n"; return 0;}

Page 23: Introduction to boost test

Boost.勉強会 #8 大阪 2312/02/11

What's the Boost.Test?

#include <iostream>#include <boost/test/included/prg_exec_monitor.hpp>

int cpp_main(int, char* []){ std::cout << "Hello, world\n"; return 0;}

>./prg_exec_monitor_test.outHello, world

no errors detected

Page 24: Introduction to boost test

24Boost.勉強会 #8 大阪12/02/11

What's the Boost.Test?

● Boost.Testとは● Execution Monitor● Program Execution Monitor● Minimal Testing Facility● Unit Test Framework

の4つの機能

Introduction

What's

Features

How to use

Conclusion

Page 25: Introduction to boost test

25Boost.勉強会 #8 大阪12/02/11

What's the Boost.Test?

● Minimal Testing Facility● テスト作成のための基本的な機能を提供。

● Testing toolsの一部を使用できる。● boost/test/minimal.hppをインクルードして、main関数の代わりにtest_main関数からプログラムを開始する。

Introduction

What's

Features

How to use

Conclusion

Page 26: Introduction to boost test

26Boost.勉強会 #8 大阪12/02/11

What's the Boost.Test?

● Minimal Testing Facility● テスト作成のための基本的な機能を提供。

● Testing toolsの一部を使用できる。● boost/test/minimal.hppをインクルードして、main関数の代わりにtest_main関数からプログラムを開始する。

Introduction

What's

Features

How to use

Conclusion

Page 27: Introduction to boost test

27Boost.勉強会 #8 大阪12/02/11

What's the Boost.Test?

● Minimal Testing Facilityで提供されているTesting tools● BOOST_CHECK(pred)

– predがfalseならエラーレポートして継続● BOOST_REQUIRE(pred)

– predがfalseならエラーレポートして中断● BOOST_ERROR(message)

– エラーレポートして継続● BOOST_FAIL(message)

– エラーレポートして中断

Introduction

What's

Features

How to use

Conclusion

Page 28: Introduction to boost test

28Boost.勉強会 #8 大阪12/02/11

What's the Boost.Test?

● Minimal Testing Facility● ヘッダオンリーで使用できるので簡単。● 一応Unit Test Framework(UTF)の方もヘッダオンリーにすることができる。しかしUTFの方は長期的に使用する場合はスタンドアロンライブラリとしてリンクすることが推奨される。

Introduction

What's

Features

How to use

Conclusion

Page 29: Introduction to boost test

Boost.勉強会 #8 大阪 2912/02/11

What's the Boost.Test?

#include <boost/test/minimal.hpp>int add(int i, int j) { return i*j; }

int test_main(int, char *[]) { BOOST_CHECK( add(2, 2) == 4 ); BOOST_CHECK( add(2, 3) == 5 ); return 0;}

>./minimal_test_test.outminimal_test_test.cpp(6):test add(2, 3) == 5 failed in function:'int test_main(int, char**)'

**** 1 error detected

Page 30: Introduction to boost test

30Boost.勉強会 #8 大阪12/02/11

What's the Boost.Test?

● Boost.Testとは● Execution Monitor● Program Execution Monitor● Minimal Testing Facility● Unit Test Framework

の4つの機能

Introduction

What's

Features

How to use

Conclusion

Page 31: Introduction to boost test

31Boost.勉強会 #8 大阪12/02/11

What's the Boost.Test?

● The Unit Test Framework(UTF)● Boost.Testのメイン機能● ヘッダオンリーあるいはスタンドアロンライブラリとして使用できる。

Introduction

What's

Features

How to use

Conclusion

Page 32: Introduction to boost test

32Boost.勉強会 #8 大阪12/02/11

What's the Boost.Test?

● Unit Testing Frameworkが満たすべき要件

● ユニットテストの作業は、プロジェクトの実装初期からメンテナンス、そして後々の改訂にいたるまで、ソフトウェア開発のさまざまな段階で発生する。

● そのためUnit Testing Frameworkに対して(時に衝突するような)多くの性質が要求される

Introduction

What's

Features

How to use

Conclusion

Page 33: Introduction to boost test

Boost.勉強会 #8 大阪 3312/02/11

Requirements of UTF

● シンプルで、使い始めの人にも分かりやすくユニットテストモジュールが書けるべき

● 上級者が自明でないようなテストをすることも可能であるべき

● テストモジュールはたくさんの小さなテストケースを持つことができ、開発者がそれをテストスイートにグループ化することができるべき

● 開発初期にはユーザーは詳細で説明的なエラーメッセージを見たい。しかし回帰テスト中にはどのテストが失敗したかだけを知りたい。

Page 34: Introduction to boost test

Boost.勉強会 #8 大阪 3412/02/11

Requirements of UTF(Cont'd)

● 小さいテストモジュールの実行時間>コンパイル時間であるべき。実行に1秒しかかからないテストのコンパイルに1分も待ちたくはない。

● 長く複雑なテストではユーザーはテストの進捗を見られるようにしたい。

● 簡単なテストは外部ライブラリを必要とするべきではない。

● 長期的な使用のためにユニットテストフレームワークはスタンドアロンなライブラリとしてビルドできるべき。

Page 35: Introduction to boost test

35Boost.勉強会 #8 大阪12/02/11

What's the Boost.Test?

● UTFの設計はこれらをベースにしている。そしてUTFは多岐に渡る機能を提供する● さまざまなTesting toolsを使用することで簡単にテストを書くことができる。

● 一つのテストツリーの中にテストケースを組織化できる。

● 面倒なエラー検出とレポーティングの義務とフレームワークの実行時パラメータ処理からあなたを解放する。などなど。ここらへんの話はhttp://www.boost.org/libs/test/doc/html/utf/intro.html に。

Introduction

What's

Features

How to use

Conclusion

Page 36: Introduction to boost test

36Boost.勉強会 #8 大阪12/02/11

What's the Boost.Test?

● 長い説明もつまらないのでちょっと使ってみる。

Introduction

What's

Features

How to use

Conclusion

Page 37: Introduction to boost test

Boost.勉強会 #8 大阪 3712/02/11

What's the Boost.Test?

 #define BOOST_TEST_MODULE SimpleTest#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_SUITE(simple_test_suite)

 BOOST_AUTO_TEST_CASE(simple_test) { BOOST_CHECK_EQUAL(2+2, 4);}

 BOOST_AUTO_TEST_SUITE_END()

Page 38: Introduction to boost test

Boost.勉強会 #8 大阪 3812/02/11

What's the Boost.Test?

//テストモジュールの定義#define BOOST_TEST_MODULE SimpleTest#include <boost/test/unit_test.hpp>

//テストスイートの定義とテストモジュールへの追加BOOST_AUTO_TEST_SUITE(simple_test_suite)

//テストケースの定義とテストスイートへの追加BOOST_AUTO_TEST_CASE(simple_test) { BOOST_CHECK_EQUAL(2+2, 4);}

BOOST_AUTO_TEST_SUITE_END()

Page 39: Introduction to boost test

12/02/11 Boost.勉強会 #8 大阪 39

What's the Features?

Page 40: Introduction to boost test

40Boost.勉強会 #8 大阪12/02/11

What's the Features?

● Unit Test Frameworkの機能● Usage Variant● テストランナー● モジュール初期化関数● テストの作成と組織化● Fixture● テストの出力● 実行時のテスト設定● Testing tools

Introduction

What's

Features

How to use

Conclusion

Page 41: Introduction to boost test

41Boost.勉強会 #8 大阪12/02/11

What's the Features?

● Unit Test Frameworkの機能● Usage Variant● テストランナー● モジュール初期化関数● テストの作成と組織化● Fixture● テストの出力● 実行時のテスト設定● Testing tools

Introduction

What's

Features

How to use

Conclusion

Page 42: Introduction to boost test

42Boost.勉強会 #8 大阪12/02/11

What's the Features?

● Usage Variant● Testをする状況や環境に合わせて4種類のUTFの使用法が用意されている。

Introduction

What's

Features

How to use

Conclusion

What's

Features

Page 43: Introduction to boost test

43Boost.勉強会 #8 大阪12/02/11

What's the Features?

● Usage Variant● Static Library Variant● UTFをスタティックリンクする構成● Macでは使えない・・・?

http://d.hatena.ne.jp/kei10in/20100623/1277294693

Introduction

What's

Features

How to use

Conclusion

What's

Features

Page 44: Introduction to boost test

44Boost.勉強会 #8 大阪12/02/11

What's the Features?

● Usage Variant● Dynamic Library Variant● UTFをダイナミックリンクする構成● #define BOOST_TEST_DYN_LINK

● (メインになる)ただひとつのソースのboost/test/unit_test.hppの、そのインクルード前にBOOST_TEST_MAINを定義する。あるいはBOOST_TEST_MODULEを使用してもOK。

Introduction

What's

Features

How to use

Conclusion

What's

Features

Page 45: Introduction to boost test

45Boost.勉強会 #8 大阪12/02/11

What's the Features?

● Usage Variant● Dynamic Library Variant● Static Library Variantを使用するとバイナリが大きくなるので、Static Library Variantを使用した大量のテストのプロジェクトがあるとストレージの容量的な問題が起こるかも。その場合にはDynamic Library Variantを使用したほう良い。

Introduction

What's

Features

How to use

Conclusion

What's

Features

Page 46: Introduction to boost test

46Boost.勉強会 #8 大阪12/02/11

What's the Features?

● Usage Variant● Single-Header Variant● UTFをヘッダオンリーにする構成● テストモジュールのソースがひとつだけの時使える。

● (コンパイル時間を考えると)長期的な使用にはスタティックリンクかダイナミックリンクを使用するべき

● #include <boost/test/included/unit_test.hpp>

Introduction

What's

Features

How to use

Conclusion

What's

Features

Page 47: Introduction to boost test

47Boost.勉強会 #8 大阪12/02/11

What's the Features?

● Usage Variant● External Test Runner Variant● ビルトインではないテストランナーを使用する構成

● テストモジュールはダイナミックリンクライブラリとして作成する

● #define BOOST_TEST_DYN_LINK

Introduction

What's

Features

How to use

Conclusion

What's

Features

Page 48: Introduction to boost test

48Boost.勉強会 #8 大阪12/02/11

What's the Features?

● Unit Test Frameworkの機能● Usage Variant● テストランナー● モジュール初期化関数● テストの作成と組織化● Fixture● テストの出力● 実行時のテスト設定● Testing tools

Introduction

What's

Features

How to use

Conclusion

Page 49: Introduction to boost test

49Boost.勉強会 #8 大阪12/02/11

What's the Features?

● テストランナー(Test Runner)● テストの実行を管理する

● テストモジュールのエントリポイント● 実行時パラメータ(コマンドライン引数など)からUTFを初期化する

● ログとテストのレポート用にOutputを準備● などなど・・・

Introduction

What's

Features

How to use

Conclusion

Features

What's

Page 50: Introduction to boost test

50Boost.勉強会 #8 大阪12/02/11

What's the Features?

● テストランナー(Test Runner)● ビルトイン以外のテストランナーを使用することができる。(先進的なテストランナーはGUIやテストカバレッジの機能を備えているかも)(でも見たことない)

● 今日は扱いません。あとよく分かりません。語りえぬものについては、沈黙しなければならない

Introduction

What's

Features

How to use

Conclusion

Features

What's

Page 51: Introduction to boost test

51Boost.勉強会 #8 大阪12/02/11

What's the Features?

● Unit Test Frameworkの機能● Usage Variant● テストランナー● モジュール初期化関数● テストの作成と組織化● Fixture● テストの出力● 実行時のテスト設定● Testing tools

Introduction

What's

Features

How to use

Conclusion

Page 52: Introduction to boost test

52Boost.勉強会 #8 大阪12/02/11

What's the Features?

● モジュール初期化関数● テストツリーを構築するなど…?● ほとんどビルトインのもので済むので使わなくても大丈夫

● 今日は扱いません。あとよく分かりません。語りえぬものについては、沈黙しなければならない

Introduction

What's

Features

How to use

Conclusion

Features

What's

Page 53: Introduction to boost test

53Boost.勉強会 #8 大阪12/02/11

What's the Features?

● Unit Test Frameworkの機能● Usage Variant● テストランナー● モジュール初期化関数● テストの作成と組織化● Fixture● テストの出力● 実行時のテスト設定● Testing tools

Introduction

What's

Features

How to use

Conclusion

Page 54: Introduction to boost test

54Boost.勉強会 #8 大阪12/02/11

What's the Features?

● テストの定義と組織化

● テストケースを作成● テストケースをテストスイートにグループ化できる

● テストスイートをさらに大きなテストスイートにグループ化できる

Introduction

What's

Features

How to use

Conclusion

Page 55: Introduction to boost test

Boost.勉強会 #8 大阪 5512/02/11

What's the Boost.Test?( 再掲 )

//テストモジュールの定義#define BOOST_TEST_MODULE SimpleTest#include <boost/test/unit_test.hpp>

//テストスイートの定義とテストモジュールへの追加BOOST_AUTO_TEST_SUITE(simple_test_suite)

//テストケースの定義とテストスイートへの追加BOOST_AUTO_TEST_CASE(simple_test) { BOOST_CHECK_EQUAL(2+2, 4);}

BOOST_AUTO_TEST_SUITE_END()

Page 56: Introduction to boost test

56Boost.勉強会 #8 大阪12/02/11

What's the Features?

● テストの定義と組織化● BOOST_AUTO_TEST_CASE(

test_case_name)

● 引数なしテストケースを定義する

● 手動でテストケースの登録を行う場合は、引数付きのテストケースを使うことができる。

Introduction

What's

Features

How to use

Conclusion

BOOST_AUTO_TEST_CASE(test_case_name) { //test body}

BOOST_AUTO_TEST_CASE(test_case_name) { //test body}

Page 57: Introduction to boost test

57Boost.勉強会 #8 大阪12/02/11

What's the Features?

● テストの定義と組織化● BOOST_AUTO_TEST_CASE_TEMPLATE(

test_case_name, formal_type_parameter_name, collection_of_types)

● テンプレートを使用したテストケースを定義する

● 同じ内容で型のみが異なるテストを書くときに便利

Introduction

What's

Features

How to use

Conclusion

Page 58: Introduction to boost test

58Boost.勉強会 #8 大阪12/02/11

What's the Features?

● テストの定義と組織化● BOOST_AUTO_TEST_SUITE(

test_suite_name)

● テストスイートを定義する

Introduction

What's

Features

How to use

Conclusion

BOOST_AUTO_TEST_SUITE(test_suite_name)

// some tests// BOOST_AUTO_TEST_CASE(test1)// { /**/ }

BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_SUITE(test_suite_name)

// some tests// BOOST_AUTO_TEST_CASE(test1)// { /**/ }

BOOST_AUTO_TEST_SUITE_END()

Page 59: Introduction to boost test

59Boost.勉強会 #8 大阪12/02/11

What's the Features?

● テストの定義と組織化● テストケースはテストスイートのなかに複数含めることができる。

● テストスイートは入れ子になることができる。

Introduction

What's

Features

How to use

Conclusion

Page 60: Introduction to boost test

Boost.勉強会 #8 大阪 6012/02/11

What's the Boost.Test?

#include <boost/test/unit_test.hpp>BOOST_AUTO_TEST_SUITE(suite1)

BOOST_AUTO_TEST_SUITE(suite2)

BOOST_AUTO_TEST_CASE(test1) { /**/ } BOOST_AUTO_TEST_CASE(test2) { /**/ }

BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_CASE(test3) { /**/ }

BOOST_AUTO_TEST_SUITE_END()

Page 61: Introduction to boost test

Boost.勉強会 #8 大阪 6112/02/11

What's the Boost.Test?

Master Test SuiteMaster Test Suite

suite2suite2

test1test1test2test2

suite1suite1

test3test3

Page 62: Introduction to boost test

62Boost.勉強会 #8 大阪12/02/11

What's the Features?

● Unit Test Frameworkの機能● Usage Variant● テストランナー● モジュール初期化関数● テストの作成と組織化● Fixture● テストの出力● 実行時のテスト設定● Testing tools

Introduction

What's

Features

How to use

Conclusion

Page 63: Introduction to boost test

63Boost.勉強会 #8 大阪12/02/11

What's the Features?

● Fixture● テストケースなどで以下の役割を担う● テスト開始前に状態を整える● テストに関する特定の状態を用意する● テスト終了後にクリーンアップをする

Introduction

What's

Features

How to use

Conclusion

Page 64: Introduction to boost test

64Boost.勉強会 #8 大阪12/02/11

What's the Features?

● Generic Fixture ModelIntroduction

What's

Features

How to use

Conclusion struct <fixture-name>{ <fixture-name>(); // setup function ~<fixture-name>(); // teardown function};

struct <fixture-name>{ <fixture-name>(); // setup function ~<fixture-name>(); // teardown function};

Page 65: Introduction to boost test

Boost.勉強会 #8 大阪 6512/02/11

What's the Boost.Test?

//Fixtureを使ったテストの例struct MyFixture { MyFixture() { i = new int; *i = 0 } ~ MyFixture() { delete i; } int * i;};BOOST_AUTO_TEST_CASE( test_case1 ){ MyFixture f; // do something involving f.i}

Page 66: Introduction to boost test

Boost.勉強会 #8 大阪 6612/02/11

What's the Boost.Test?

//Fixtureを使ったテストの例struct MyFixture { MyFixture() { i = new int; *i = 0 } ~ MyFixture() { delete i; } int * i;};BOOST_AUTO_TEST_CASE( test_case1 ){ MyFixture f; // do something involving f.i}

Page 67: Introduction to boost test

Boost.勉強会 #8 大阪 6712/02/11

What's the Boost.Test?

struct MyFixture { MyFixture() { i = new int; *i = 0 } ~ MyFixture() { delete i; } int * i;};BOOST_FIXTURE_TEST_CASE( test_case1, MyFixture ){ // do something involving i}

// test_cast1を開始する前にMyFixtureを構築して、 // test_case1が終わるとMyFixtureを破棄する

Page 68: Introduction to boost test

68Boost.勉強会 #8 大阪12/02/11

What's the Features?

● Fixture● テストケースなどで以下の役割を担う● テスト開始前に状態を整える● テストに関する特定の状態を用意する● テスト終了後にクリーンアップをする

Introduction

What's

Features

How to use

Conclusion

Page 69: Introduction to boost test

69Boost.勉強会 #8 大阪12/02/11

What's the Features?

● Fixture● 3つのレベルでSetupとCleanupを行うことができる● テストケース● テストスイート● グローバル

Introduction

What's

Features

How to use

Conclusion

Page 70: Introduction to boost test

70Boost.勉強会 #8 大阪12/02/11

What's the Features?

● Fixtureの設定● BOOST_FIXTURE_TEST_CASE(

test_case_name, fixure_name)

● テストケースごとのFixtureを設定

Introduction

What's

Features

How to use

Conclusion

Page 71: Introduction to boost test

71Boost.勉強会 #8 大阪12/02/11

What's the Features?

● Fixtureの設定● BOOST_FIXTURE_TEST_SUITE(

test_suite_name, fixure_name)

● テストスイートごとのFixtureを設定● テストスイート内の各テストケースが同じFixtureを使用したいときの為にテストスイートレベルでFixtureを使用できる。

Introduction

What's

Features

How to use

Conclusion

Page 72: Introduction to boost test

72Boost.勉強会 #8 大阪12/02/11

What's the Features?

● Fixtureの設定● BOOST_GLOBAL_FIXTURE(

fixure_name)● グローバルなFixtureを設定● テストの開始時の初期化を担う● テスト用の各ソースファイルに複数の別のグローバルなFixtureを設定することも可能

Introduction

What's

Features

How to use

Conclusion

Page 73: Introduction to boost test

73Boost.勉強会 #8 大阪12/02/11

What's the Features?

● Fixtureの設定● BOOST_GLOBAL_FIXTURE(

fixure_name)● グローバルなFixtureを設定● テストの開始時の初期化を担う● テスト用の各ソースファイルに複数の別のグローバルなFixtureを設定することも可能

● でもデストラクト順が厄介・・・?

Introduction

What's

Features

How to use

Conclusion

Page 74: Introduction to boost test

Boost.勉強会 #8 大阪 7412/02/11

What's the Boost.Test?

struct F1 { F1() { std::cout << "F1" << std::endl; } ~F1() { std::cout << "~F1" << std::endl; }};

//同様にF2, F3を定義する

Page 75: Introduction to boost test

Boost.勉強会 #8 大阪 7512/02/11

What's the Boost.Test?

BOOST_GLOBAL_FIXTURE(F1);BOOST_GLOBAL_FIXTURE(F2);BOOST_GLOBAL_FIXTURE(F3);

BOOST_AUTO_TEST_SUITE(global_fixture_test_suite)

BOOST_AUTO_TEST_CASE(global_fixture_test){ //nop}

BOOST_AUTO_TEST_SUITE_END()

Page 76: Introduction to boost test

Boost.勉強会 #8 大阪 7612/02/11

What's the Boost.Test?

BOOST_GLOBAL_FIXTURE(F1);BOOST_GLOBAL_FIXTURE(F2);BOOST_GLOBAL_FIXTURE(F3);

BOOST_AUTO_TEST_SUITE(global_fixture_test_suite)

BOOST_AUTO_TEST_CASE(global_fixture_test){ //nop}

BOOST_AUTO_TEST_SUITE_END()

F1F2F3Running 1 test case...~F1~F2~F3

Page 77: Introduction to boost test

77Boost.勉強会 #8 大阪12/02/11

What's the Features?

● Unit Test Frameworkの機能● Usage Variant● テストランナー● モジュール初期化関数● テストの作成と組織化● Fixture● テストの出力● 実行時のテスト設定● Testing tools

Introduction

What's

Features

How to use

Conclusion

Page 78: Introduction to boost test

78Boost.勉強会 #8 大阪12/02/11

What's the Features?

● テストの出力● 全てのテストで統一的なレポートを● エラーについて、ソース上の詳細な

情報を● テストのエラーの記述(test log)はテスト結果の要約(test results report)とは分離する

● 柔軟な出力内容● 柔軟な出力の形式

Introduction

What's

Features

How to use

Conclusion

Page 79: Introduction to boost test

79Boost.勉強会 #8 大阪12/02/11

What's the Features?

● テストの出力● Test Log● Test Report● Progress Display

Introduction

What's

Features

How to use

Conclusion

Page 80: Introduction to boost test

80Boost.勉強会 #8 大阪12/02/11

What's the Features?

● テストの出力● Test Log● Test Report● Progress Display

Introduction

What's

Features

How to use

Conclusion

Page 81: Introduction to boost test

81Boost.勉強会 #8 大阪12/02/11

What's the Features?

● テストの出力● Test Logのレベル

● Success information messages● Test tree traversal notifications● General information messages● Warning messages● Non fatal error messages● Uncaught C++ exceptions notifications● Non-fatal system error● Fatal system error

Introduction

What's

Features

How to use

Conclusion

Page 82: Introduction to boost test

82Boost.勉強会 #8 大阪12/02/11

What's the Features?

● テストの出力● Test Logのレベル● 各レベルは下のレベルの内容を含む● デフォルトはNon fatal error messages

● 各レベルの詳細はhttp://www.boost.org/libs/test/doc/html/utf/user-guide/test-output/test-log.html

を参照。

Introduction

What's

Features

How to use

Conclusion

Page 83: Introduction to boost test

83Boost.勉強会 #8 大阪12/02/11

What's the Features?

● テストの出力● operator<<のインターフェースを持たない型がlogに出力されようとしたときはコンパイルエラーになる。

● そのコンパイルエラーを避ける場合、あるいはlogに出力したくない場合はBOOST_TEST_DONT_PRINT_LOG_VALUE( ArgumentType )

を使用する

Introduction

What's

Features

How to use

Conclusion

Page 84: Introduction to boost test

84Boost.勉強会 #8 大阪12/02/11

What's the Features?

● テストの出力BOOST_TEST_MESSAGE( test_message )

● Logにメッセージをさしこむ● ただし、デフォルトのLogレベルの設定では出力されない

● LogレベルをGeneral information messages

以上にする

Introduction

What's

Features

How to use

Conclusion

Page 85: Introduction to boost test

85Boost.勉強会 #8 大阪12/02/11

What's the Features?

● テストの出力● Test Logのレベル● 各レベルの詳細は

http://www.boost.org/libs/test/doc/html/utf/user-guide/test-output/test-log.html

を参照。

Introduction

What's

Features

How to use

Conclusion

Page 86: Introduction to boost test

86Boost.勉強会 #8 大阪12/02/11

What's the Features?

● テストの出力BOOST_TEST_CHECKPOINT( checkpoint_message)

● Logに名前付きのチェックポイントをさしこむ

Introduction

What's

Features

How to use

Conclusion

Page 87: Introduction to boost test

87Boost.勉強会 #8 大阪12/02/11

What's the Features?

● テストの出力● Logの出力フォーマット

● Human Readable– Microsoft系C++コンパイラのエラー記述に似た形式でLogを出力

● XML– XML形式でLogを出力

Introduction

What's

Features

How to use

Conclusion

Page 88: Introduction to boost test

88Boost.勉強会 #8 大阪12/02/11

What's the Features?

● テストの出力● Logの出力先をコンパイル時に設定

unit_test_log.set_stream( std::ostream& str );

Introduction

What's

Features

How to use

Conclusion

Page 89: Introduction to boost test

Boost.勉強会 #8 大阪 8912/02/11

What's the Boost.Test?

struct MyConfig { MyConfig() : test_log( "example.log" ) { boost::unit_test::unit_test_log.set_stream( test_log ); } ~MyConfig() { boost::unit_test::unit_test_log.set_stream( std::cout ); } std::ofstream test_log;};

BOOST_GLOBAL_FIXTURE( MyConfig );BOOST_AUTO_TEST_CASE( test_case ){

//...

Page 90: Introduction to boost test

90Boost.勉強会 #8 大阪12/02/11

What's the Features?

● テストの出力● Log出力のレベルをコンパイル時に設定するunit_test_log.set_threshold_level( boost::unit_test::log_level );

Introduction

What's

Features

How to use

Conclusion

Page 91: Introduction to boost test

Boost.勉強会 #8 大阪 9112/02/11

What's the Features?

using namespace boost::unit_test;BOOST_AUTO_TEST_CASE( test_case0 ){

if ( runtime_config::log_level() < log_warnings ) unit_test_log.set_threshold_level( log_warnings ); BOOST_WARN( sizeof(int) > 4 );}

Page 92: Introduction to boost test

92Boost.勉強会 #8 大阪12/02/11

What's the Features?

● テストの出力● Test Log● Test Report● Progress Display

Introduction

What's

Features

How to use

Conclusion

Page 93: Introduction to boost test

93Boost.勉強会 #8 大阪12/02/11

What's the Features?

● テストの出力● テストレポートの出力

● Runtime configuration● Compile time configuration● Report output stream redirection and access● Report level configuration● Predefined report format selection● Custom report format support

Introduction

What's

Features

How to use

Conclusion

Page 94: Introduction to boost test

94Boost.勉強会 #8 大阪12/02/11

What's the Features?

● テストの出力● テストレポートの出力

● Runtime configuration● Compile time configuration● Report output stream redirection and access● Report level configuration● Predefined report format selection● Custom report format support

● ドキュメントに詳細がない・・・?

Introduction

What's

Features

How to use

Conclusion

Page 95: Introduction to boost test

95Boost.勉強会 #8 大阪12/02/11

What's the Features?

● テストの出力● Test Log● Test Report● Progress Display

Introduction

What's

Features

How to use

Conclusion

Page 96: Introduction to boost test

96Boost.勉強会 #8 大阪12/02/11

What's the Features?

● テストの出力● Progress Display● 次に紹介する実行時設定

show_progressを使用する。

Introduction

What's

Features

How to use

Conclusion

> example --show_progress=yes --log_level=nothing

0% 10 20 30 40 50 60 70 80 90 100%|----|----|----|----|----|----|----|----|----|----|***************************************************

*** No errors detected

Page 97: Introduction to boost test

97Boost.勉強会 #8 大阪12/02/11

What's the Features?

● Unit Test Frameworkの機能● Usage Variant● テストランナー● モジュール初期化関数● テストの作成と組織化● Fixture● テストの出力● 実行時のテスト設定● Testing tools

Introduction

What's

Features

How to use

Conclusion

Page 98: Introduction to boost test

98Boost.勉強会 #8 大阪12/02/11

What's the Features?

● 実行時のテスト設定● 環境変数、あるいは実行時引数でログ出力の挙動を設定する● auto_start_dbg● build_info● catch_system_errors● detect_memory_leak● detect_fp_exceptions

Introduction

What's

Features

How to use

Conclusion

Page 99: Introduction to boost test

99Boost.勉強会 #8 大阪12/02/11

What's the Features?

● 実行時のテスト設定● 環境変数、あるいは実行時引数でログ出力の挙動を設定する● log_format● log_level● output_format● random● report_format

Introduction

What's

Features

How to use

Conclusion

Page 100: Introduction to boost test

100Boost.勉強会 #8 大阪12/02/11

What's the Features?

● 実行時のテスト設定● 環境変数、あるいは実行時引数でログ出力の挙動を設定する● report_level● result_code● run_test● show_progress● use_alt_stack

Introduction

What's

Features

How to use

Conclusion

Page 101: Introduction to boost test

101Boost.勉強会 #8 大阪12/02/11

What's the Features?

● Unit Test Frameworkの機能● Usage Variant● テストランナー● モジュール初期化関数● テストの作成と組織化● Fixture● テストの出力● 実行時のテスト設定● Testing tools

Introduction

What's

Features

How to use

Conclusion

Page 102: Introduction to boost test

102Boost.勉強会 #8 大阪12/02/11

What's the Features?

● Testing tools● Minimal Testing Facilityの例で使用したBOOST_CHECKなど

Introduction

What's

Features

How to use

Conclusion

Page 103: Introduction to boost test

103Boost.勉強会 #8 大阪12/02/11

What's the Features?

● Testing tools● 複数のマクロと関数宣言からなるツール集。

● テストの作成と管理を容易にして、統一的なエラーレポーティングの仕組みを提供する。

● 全てのツールは自動的にエラーレポーティングにエラーの位置情報(ファイル名と行番号)

Introduction

What's

Features

How to use

Conclusion

Page 104: Introduction to boost test

104Boost.勉強会 #8 大阪12/02/11

What's the Features?

● Testing tools flavors● 全てのテストツールにはflavorsが提供されている

● テストツールのアサーションについてのレベル的なもの

Introduction

What's

Features

How to use

Conclusion

Page 105: Introduction to boost test

105Boost.勉強会 #8 大阪12/02/11

What's the Features?

● Testing tools flavors● WARN

● 警告● エラーカウント:そのまま● テストの実行:継続

Introduction

What's

Features

How to use

Conclusion

Page 106: Introduction to boost test

106Boost.勉強会 #8 大阪12/02/11

What's the Features?

● Testing tools flavors● CHECK

● エラーチェック● エラーカウント:増加● テストの実行:継続

Introduction

What's

Features

How to use

Conclusion

Page 107: Introduction to boost test

107Boost.勉強会 #8 大阪12/02/11

What's the Features?

● Testing tools flavors● REQUIRE

● 必要条件チェック● エラーカウント:増加● テストの実行:中断

Introduction

What's

Features

How to use

Conclusion

Page 108: Introduction to boost test

108Boost.勉強会 #8 大阪12/02/11

What's the Features?

● 提供されているTesting tools● BOOST_<level>

Introduction

What's

Features

How to use

Conclusion

BOOST_CHECK( i == 1 );BOOST_REQUIRE( !str.empty() );BOOST_CHECK( i == 1 );BOOST_REQUIRE( !str.empty() );

Running 1 test case...testing_tool_test.cpp:10: error in "testing_tool_test": check i == 1 failedtesting_tool_test.cpp:11: fatal error in "testing_tool_test": critical check !str.empty() failed

*** 2 failures detected in test suite "Master Test Suite"

Page 109: Introduction to boost test

109Boost.勉強会 #8 大阪12/02/11

What's the Features?

● 提供されているTesting tools● BOOST_<level>_EQUAL

Introduction

What's

Features

How to use

Conclusion

BOOST_CHECK_EQUAL( i, 1 );BOOST_REQUIRE_EQUAL( str, “STRING” );BOOST_CHECK_EQUAL( i, 1 );BOOST_REQUIRE_EQUAL( str, “STRING” );

Running 1 test case...testing_tool_test.cpp:10: error in "testing_tool_test": check i == 1 failed [0 != 1]testing_tool_test.cpp:11: fatal error in "testing_tool_test": critical check str == "STRING" failed [ != STRING]

*** 2 failures detected in test suite "Master Test Suite"

Page 110: Introduction to boost test

110Boost.勉強会 #8 大阪12/02/11

What's the Features?

● 提供されているTesting tools● などなど・・・● 詳しくはドキュメントに書いてあります。

Introduction

What's

Features

How to use

Conclusion

Page 111: Introduction to boost test

111Boost.勉強会 #8 大阪12/02/11

What's the Features?

● 提供されているTesting tools● Testing toolsを使うことでテストの意味を分かりやすく書くことができ、さらに統一的なエラーレポーティングができるようになる。

Introduction

What's

Features

How to use

Conclusion

Page 112: Introduction to boost test

12/02/11 Boost.勉強会 #8 大阪 112

How to use the Boost.Test

Page 113: Introduction to boost test

113Boost.勉強会 #8 大阪12/02/11

How to use the Boost.Test

● デモIntroduction

What's

Features

How to use

Conclusion

Page 114: Introduction to boost test

12/02/11 Boost.勉強会 #8 大阪 114

Conclusion

Page 115: Introduction to boost test

115Boost.勉強会 #8 大阪12/02/11

Conclusion

● Boost.Testを使うことで、C++のテストを自動的に組織しながらテストを書くことができる。

● 実行時引数を設定することで、柔軟にテストをすることができる。

● Testing toolsを使うことでテスト内容を明確にすることができる。

● (Boostに含まれてるので)普段からBoostを使っている人は導入しやすい。

Introduction

What's

Features

How to use

Conclusion

Page 116: Introduction to boost test

116Boost.勉強会 #8 大阪12/02/11

Conclusion

● 欠点● 実行時にスキップするテストを選ぶことが出来ない

● Mockの仕組みがない● テスト出力のストリームがワイド文字列対応していない

Introduction

What's

Features

How to use

Conclusion

Page 117: Introduction to boost test

117Boost.勉強会 #8 大阪12/02/11

Conclusion

● 欠点● 実行時にスキップするテストを選ぶことが出来ない

● Mockの仕組みがない● テスト出力のストリームがワイド文字列対応していない

Introduction

What's

Features

How to use

Conclusion

Page 118: Introduction to boost test

118Boost.勉強会 #8 大阪12/02/11

Conclusion

● Mockの仕組みがないhttp://alexott.net/en/cpp/CppTestingIntro.html

● ここにGoogle MockとBoost.Testを組み合わせたテストの紹介があります。

Introduction

What's

Features

How to use

Conclusion

Page 119: Introduction to boost test

119Boost.勉強会 #8 大阪12/02/11

Conclusion

● Mockの仕組みがない● アンドキュメントだがあるらしい・・・?(@cpp_akiraさん情報ありがとうございます)

● see boost/libs/test/example/example/est_example1.cpp

● see boost/libs/test/example/example/est_example2.cpp

Introduction

What's

Features

How to use

Conclusion

Page 120: Introduction to boost test

120Boost.勉強会 #8 大阪12/02/11

Conclusion

● 参考文献/サイト● http://www.boost.org/libs/test/doc/html/

● http://alexott.net/en/cpp/CppTestingIntro.html

● http://www.alittlemadness.com/2009/03/31/c-unit-testing-with-boosttest/

● http://blog.livedoor.jp/naoya_t/archives/51043392.html

Introduction

What's

Features

How to use

Conclusion

Page 121: Introduction to boost test

121Boost.勉強会 #8 大阪12/02/11

Conclusion

● 質問など・・・Introduction

What's

Features

How to use

Conclusion

Page 122: Introduction to boost test

12/02/11 Boost.勉強会 #8 大阪 122

Thank You!

ありがとうございました!