Top Banner
JUnit実践入門 渡辺 修司, ”JUnit実践入門 体系的に学ぶユニットテストの技法”, 技術評論社, 2012 第1章〜第3章より 平山テスト太郎
21

About junit

Jul 18, 2015

Download

Technology

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: About junit

JUnit実践入門渡辺 修司, ”JUnit実践入門 体系的に学ぶユニットテストの技法”, 技術評論社, 2012

第1章〜第3章より

平山テスト太郎

Page 2: About junit

ユニットテスト?

• ”ユニット”が想定した想定通りの振る舞いをするか検証

• クラス、メソッド単位

• システムの単機能

Page 3: About junit

ユニットテスト?

• 自分が書いたコードが要求仕様どおり正しく動くか検証

• ☓ 仕様バグ・仕様誤解

• 自分が書いたコードに責任をもつ

• 安心して眠りにつける

Page 4: About junit

JUnit(xUnit)

• ユニットテストを行うテスティングフレームワーク

• 自動化テストの仕組みを提供

• テストケース記述法

• アサーションAPI

• テストランナー

• テストフィクスチャ

Page 5: About junit

JUnit(xUnit)

• Kent BeckのSUnitが源流

• 継続的インテグレーション、アジャイルの一大要素

Page 6: About junit

JUnitによるユニットテスト記述

Page 7: About junit

JUnitによるユニットテスト記述

• テストケース(テスト項目)

• 下記3つを定義・検証するもの

• テストの前提条件

• テスト対象に与えるの入力

• 期待される結果

• 「ある状況で、ある入力を与えた時に期待される結果」を検証するコード

Page 8: About junit

JUnitによるユニットテスト記述

• テストクラスにテストケースを定義

• public メソッド

• @Testアノテーション

• 返り値void、引数なし

• テスト内容を表すメソッド名

Page 9: About junit

JUnitによるユニットテスト記述

• https://github.com/shuji/practice-junit/tree/master/junit-tutorial

Page 10: About junit

JUnitによるユニットテストのパターン

• 1. 標準的な振る舞いの検証

• 2. 例外送出検証

• 3. コンストラクタの検証

Page 11: About junit

JUnitによるユニットテストのパターン

• 1. 標準的な振る舞いの検証

• (大抵)4フェーズで構成される

• 4フェーズテスト

• 初期化/テスト実行/結果検証/後処理

Page 12: About junit

JUnitによるユニットテストのパターン

• 1. 標準的な振る舞いの検証

• テストケース間で共通する初期化/後処理の記述

• @Before,@Afterアノテーションを付与したメソッド

• (若しくはsetUp(), tearDown())

• テストケース毎に毎回呼ばれる

• https://phpunit.de/manual/current/ja/appendixes.annotations.html#appendixes.annotations.before

• https://github.com/shuji/practice-junit/blob/b4674819f8bb59d63fbca1621cde6198f380c88d/junit-exercises/src/test/java/ch18/ex03/CounterTest.java

Page 13: About junit

JUnitによるユニットテストのパターン

• 1. 標準的な振る舞いの検証

• テストケース間で共通する初期化/後処理の記述2

• @BeforeClass,@AfterClassアノテーションを付与したメソッド

• テストクラスの1つ目のテストメソッド実行前/すべてのテストメソッド実行後に一度だけ呼ばれる

• https://phpunit.de/manual/current/ja/appendixes.annotations.html#appendixes.annotations.beforeClass

• https://github.com/shuji/practice-junit/blob/b4674819f8bb59d63fbca1621cde6198f380c88d/junit-examples/src/test/java/ch03/BeforeClassAndAfterClassTest.java

Page 14: About junit

JUnitによるユニットテストのパターン

• 2. 例外送出検証

• 異常時に期待された例外がスローされるか検証

• アノテーションに期待する例外を記載

• phpunit => @expectedException

• https://phpunit.de/manual/current/ja/writing-tests-for-phpunit.html#writing-tests-for-phpunit.exceptions

• https://phpunit.de/manual/current/ja/writing-tests-for-phpunit.html#writing-tests-for-phpunit.exceptions

Page 15: About junit

JUnitによるユニットテストのパターン

• 3. コンストラクタの検証

• インスタンス生成後に正しくフィールドが初期化されているか検証

• https://github.com/shuji/practice-junit/blob/b4674819f8bb59d63fbca1621cde6198f380c88d/junit-examples/src/test/java/ch03/ConstructorTest.java

Page 16: About junit

わかりやすいテストコードのために

Page 17: About junit

わかりやすいテストコードのために 〜用語〜

• テスト対象

• SUT(System Under Test)

• テスト対象のインスタンスの変数名を「sut」にすると、理解しやすい(らしい)

• Clazz sut = new Class()

Page 18: About junit

わかりやすいテストコードのために 〜用語〜

• 実測値/期待値

• expected/actual

• expected = “expected”;

• actual = sut.foo();

• assertEquals(actula, expected);

Page 19: About junit

わかりやすいテストコードのために 〜副作用をなくす〜

• メソッドが戻り値を持つ

• 例:lfSelectFoo()がSelect文の結果をreturn

• オブジェクトの内部状態を確認するしなくて済む

• ☓ $this->arrResultに代入

Page 20: About junit

わかりやすいテストコードのために 〜副作用をなくす〜

• 同じ引数で実行 ⇒ 常に同じ結果

• 参照透過性

• 現在時刻などの何らかの変動する実測値・外部状態を使用している

• ⇒モックオブジェクトなどを使う必要性

Page 21: About junit

わかりやすいテストコードのために 〜副作用をなくす〜

• メソッド呼び出しの結果、副作用がない

• オブジェクトの内部状態の変更などがない

• ☓ lfCheckError()で関係ないフィールドを書換え