PHPerのための「PHPUnit について語り合う」PHP TechCafe

PHPers News

PHPUnit

参考資料

PHPUnitのアサーションメソッドを知ろう! - RAKUS Developers Blog

テストとは

  • 品質を担保するための工程
  • プログラムが期待通りに動いているかの確認
  • たとえば
    • 正しい挙動をしているか
    • 負荷をかけた時は正常に動くか
    • 部品を合わせた時に正しく動くか

Unit テストとは

  • 単体テスト
  • クラスや関数などの単位で動作を確認するテスト
  • 画面遷移での動作確認が不要

PHPUnit とは

PHPプログラムとして作成可能なユニットテスト

PHPUnit マニュアル

その他のPHP系のユニットテストツール

実際にはどう使っている??

利用方法

参考書籍などの紹介

TDD

PHPUnitアサーションメソッド

PHPUnitのアサーションメソッドを知ろう! - RAKUS Developers Blog

アサーションメソッドアノテーション を組み合わせることでテスト対象のクラスや関数が期待通りに動作しているかを確認する

  • アサーションメソッド
    • 値を比較、検査して想定通りの値になっているかを確認
<?php use PHPUnit\Framework\TestCase; // PHPUnit での配列操作のテスト(PHPUnit のドキュメントより転載) class StackTest extends TestCase { public function testPushAndPop() { $stack = []; $this->assertSame(0, count($stack)); array_push($stack, 'foo'); $this->assertSame('foo', $stack[count($stack)-1]); $this->assertSame(1, count($stack)); $this->assertSame('foo', array_pop($stack)); $this->assertSame(0, count($stack)); } }
  • アノテーション
    • テストメソッド間の依存性を明示的に宣言する
<?php use PHPUnit\Framework\TestCase; // @depends アノテーションを使った依存性の表現(PHPUnit のドキュメントより転載) class StackTest extends TestCase { public function testEmpty() { $stack = []; $this->assertEmpty($stack); return $stack; } /** * @depends testEmpty */ public function testPush(array $stack) { array_push($stack, 'foo'); $this->assertSame('foo', $stack[count($stack)-1]); $this->assertNotEmpty($stack); return $stack; } /** * @depends testPush */ public function testPop(array $stack) { $this->assertSame('foo', array_pop($stack)); $this->assertEmpty($stack); } }

アサーションメソッド

assertEquals

  • ある値が期待した値と等しいかを判定
    • 比較時に暗黙の型変換が行われるため、あまり利用しない方が良い
// 以下のアサーションは全て成功になる $this->assertEquals(1, '1'); $this->assertEquals(null, ''); $this->assertEquals(null, false); $this->assertEquals(null, 0); $this->assertEquals('1', true);

assertSame

  • assertEquals とは異なり、型の厳密な比較が可能なアサーションメソッド
    • 基本的にこちらを利用する方針で問題ないはず
$this->assertSame('hoge', 'hoge'); // OK $this->assertSame(0, 0); // OK $this->assertSame(false, false); // OK $this->assertSame('hoge', 'fuga'); // NG $this->assertSame(0, false); // NG $this->assertSame(7, '7'); // NG

assertTrue, assertFalse, assertNull

  • treu, false, null を判定するアサーション
    • テスト結果の確認には assertSame を利用するよりわかりやすい
$actualValue = false; // 以下はいずれも失敗になる $this->assertTrue($actualValue); $this->assertSame(true, $actualValue);

以下はテスト失敗時のメッセージ

# assertSame(true, false) のメッセージ
Failed asserting that false is identical to true.

# assertTrue(false) のメッセージ
Failed asserting that false is true.

assertCount

  • 配列や countable なオブジェクトwの要素数を検査するアサーション
    • こちらも assertSame よりもテストの結果がわかりやすい
// 以下はいずれも失敗になる $this->assertCount(1, []); $this->asserSame(1, count([]));
# assertCount(1, []) のメッセージ
Failed asserting that actual size 0 matches expected size 1.

# assertSame(1, count([])) のメッセージ
Failed asserting that 0 is identical to 1.

assertStringStartsWith, assertStringEndsWith, assertStringContainsString

  • 文字列検査系のアサーション
    • assertSameだと煩雑になるテストが簡単に書けるようになる
// 以下2つは同じ内容のテスト $this->assertStringStartsWith('foo', $string); $this->assertSame(0, strpos($string, 'foo')); // 何をテストしたいのか理解しにくい // 以下2つは同じ内容のテスト $this->assertStringEndsWith('bar', $string); $this->assertSame('bar', substr($string, -3)); // これも理解しにくい

assertRegExp

  • 正規表現が利用できる文字列検査
    • 上記の assertStringContainsString よりも複雑な上限のテストが作成可能

assertIsXXX, assertInstanceOf

  • 値が配列かどうかの検査方法

    • assertTrue(is_array($value))
    • assertIsArray($value)
  • その他にも以下が存在

    • is_int
    • is_numeric
    • is_string
    • is_bool
    • is_callable
    • is_object
  • オブジェクトの型検査

    • assertInstanceOf(SomeClass::class, $value)
Select a repo