| Prev | Next |
既存のコードのテストを記述する際は、 以下のようなコードを何度となく繰り返し記述することになるでしょう。
public function testMethod()
{
}
PHPUnit は、既存のクラスのコードを解析して テストケースクラスの雛形を作成することができます。
次の例は、Calculator という名前のクラス (例 17.1 を参照ください) 用のテストクラスの雛形を作成する手順を示すものです。
phpunit --skeleton-test Calculator
PHPUnit 3.5.13 by Sebastian Bergmann.
Wrote test class skeleton for Calculator to CalculatorTest.php.
もとのクラスの各メソッドについて、 出来上がったテストケースクラスのテストケースは不完全な状態 (第 10 章 を参照ください) です。
名前空間 内で宣言されたクラス用のコードを雛形ジェネレータで生成する際には、 そのクラスの修飾名とクラスが宣言されているソースファイルへのパスを渡さなければなりません。
たとえば、名前空間 Foo の中でクラス Bar が宣言されている場合は 次のようにして雛形ジェネレータを実行します。
phpunit --skeleton-test "Foo\Bar" Bar.php
作成されたテストケースクラスを実行した結果を以下に示します。
phpunit --verbose CalculatorTest
PHPUnit 3.5.13 by Sebastian Bergmann.
I
Time: 0 seconds, Memory: 4.00Mb
There was 1 incomplete test:
1) CalculatorTest::testAdd
This test has not been implemented yet.
/home/sb/CalculatorTest.php:41
OK, but incomplete or skipped tests!
Tests: 1, Assertions: 0, Incomplete: 1.
@assert アノテーションを メソッドのコメント部で使用すると、 シンプルではあるけれど意味のあるテストを自動的に生成することができます。 これは不完全なテストケースではありません。 例 17.2 に例を示します。
例 17.2: @assert アノテーションをつけた Calculator クラス
<?php
class Calculator
{
/**
* @assert (0, 0) == 0
* @assert (0, 1) == 1
* @assert (1, 0) == 1
* @assert (1, 1) == 2
*/
public function add($a, $b)
{
return $a + $b;
}
}
?>
もとのクラスの各メソッドについて、 @assert アノテーションの内容をチェックします。 これらは、以下のようなテストコードに変換されます。
/**
* Generated from @assert (0, 0) == 0.
*/
public function testAdd() {
$o = new Calculator;
$this->assertEquals(0, $o->add(0, 0));
}
作成されたテストケースクラスを実行した結果を以下に示します。
phpunit CalculatorTest
PHPUnit 3.5.13 by Sebastian Bergmann.
....
Time: 0 seconds
OK (4 tests, 4 assertions)
表 17.1 に、サポートされる @assert の種類と、それがどのようなテストコードに変換されるかをまとめました。
表17.1 サポートされる @assert アノテーション
| アノテーション | 変換後の内容 |
|---|---|
@assert (...) == X
|
assertEquals(X, method(...))
|
@assert (...) != X
|
assertNotEquals(X, method(...))
|
@assert (...) === X
|
assertSame(X, method(...))
|
@assert (...) !== X
|
assertNotSame(X, method(...))
|
@assert (...) > X
|
assertGreaterThan(X, method(...))
|
@assert (...) >= X
|
assertGreaterThanOrEqual(X, method(...))
|
@assert (...) < X
|
assertLessThan(X, method(...))
|
@assert (...) <= X
|
assertLessThanOrEqual(X, method(...))
|
@assert (...) throws X
|
@expectedException X
|
テスト駆動開発 (第 13 章 を参照ください) ではまずテストを書いてからそのテストの対象となるコードを書くことになりますが、 PHPUnit ではテストケースクラスをもとにしてクラスの雛形を作成することができます。
規約に従って、Unit クラスのテストは UnitTest クラスに記述されることになります。 このテストケースクラスのソースを検索し、 Unit クラスのオブジェクトを参照している変数を見つけて そのオブジェクトがどんなメソッドをコールしているかを調べます。 例として 例 17.4 を見てみましょう。これは、例 17.3 の解析結果をもとにして作成されたものです。
例 17.3: BowlingGameTest クラス
<?php
class BowlingGameTest extends PHPUnit_Framework_TestCase
{
protected $game;
protected function setUp()
{
$this->game = new BowlingGame;
}
protected function rollMany($n, $pins)
{
for ($i = 0; $i < $n; $i++) {
$this->game->roll($pins);
}
}
public function testScoreForGutterGameIs0()
{
$this->rollMany(20, 0);
$this->assertEquals(0, $this->game->score());
}
}
?>
例 17.4: 作成された BowlingGame クラスの雛形
<?php
/**
* Generated by PHPUnit on 2008-03-10 at 17:18:33.
*/
class BowlingGame
{
/**
* @todo Implement roll().
*/
public function roll()
{
// Remove the following line when you implement this method.
throw new RuntimeException('Not yet implemented.');
}
/**
* @todo Implement score().
*/
public function score()
{
// Remove the following line when you implement this method.
throw new RuntimeException('Not yet implemented.');
}
}
?>
| Prev | Next |
assertArrayHasKey()
assertClassHasAttribute()
assertClassHasStaticAttribute()
assertContains()
assertContainsOnly()
assertEmpty()
assertEqualXMLStructure()
assertEquals()
assertFalse()
assertFileEquals()
assertFileExists()
assertGreaterThan()
assertGreaterThanOrEqual()
assertInstanceOf()
assertInternalType()
assertLessThan()
assertLessThanOrEqual()
assertNull()
assertObjectHasAttribute()
assertRegExp()
assertStringMatchesFormat()
assertStringMatchesFormatFile()
assertSame()
assertSelectCount()
assertSelectEquals()
assertSelectRegExp()
assertStringEndsWith()
assertStringEqualsFile()
assertStringStartsWith()
assertTag()
assertThat()
assertTrue()
assertType()
assertXmlFileEqualsXmlFile()
assertXmlStringEqualsXmlFile()
assertXmlStringEqualsXmlString()
Copyright © 2005-2012 Sebastian Bergmann.