| Prev | Next |
たいていの場合は、PHPUnit の API は単純なものです。単に PHPUnit_Framework_TestCase を継承したテストケースを作成し、 assertTrue() あるいは assertEquals() をコールすればよいのです。しかし、PHPUnit をより深く知りたい方のために、 ここではすべてのクラスおよび公開メソッドを説明します。
ほとんどの場合、PHPUnit を使用する際には以下の 5 つのクラスやインターフェイスに出会うことになるでしょう。
PHPUnit_Framework_Assert
実際の値が想定した値どおりかどうかを調べるための静的メソッドを集めたもの。
PHPUnit_Framework_Test
テストケースとして動作するすべてのオブジェクトのインターフェイス。
PHPUnit_Framework_TestCase
ひとつのテスト。
PHPUnit_Framework_TestSuite
テストの集まり。
PHPUnit_Framework_TestResult
ひとつあるいは複数のテストの実行結果をまとめたもの。
PHPUnit の、5 つの基本クラス/インターフェイスである PHPUnit_Framework_Assert、 PHPUnit_Framework_Test、 PHPUnit_Framework_TestCase、 PHPUnit_Framework_TestSuite およびand PHPUnit_Framework_TestResult の関係を 図 21.1 に示します。
PHPUnit 用に書かれたテストケースのほとんどは、間接的に PHPUnit_Framework_Assert を継承しています。ここには、 値を自動的にチェックして矛盾を報告するためのメソッドが含まれています。 これらのメソッドは静的に宣言されているので、 あなたが作成したメソッドの中で「規約による設計」方式のアサーションを使用し、 PHPUnit に結果を報告させることができます (例 21.1 を参照ください)。
例 21.1: 「規約による設計」方式のアサーション
<?php
require_once 'PHPUnit/Framework.php';
class Sample
{
public function aSampleMethod($object)
{
PHPUnit_Framework_Assert::assertNotNull($object);
}
}
$sample = new Sample;
$sample->aSampleMethod(NULL);
?>
Fatal error: Uncaught exception 'PHPUnit_Framework_ExpectationFailedException' with message 'Failed asserting that <null> is not identical to <null>'.
しかし、ほとんどの場合はこれらのアサーションはテストの中で行います。
各アサーションメソッドには 2 種類の方式があります。 エラー時に表示されるメッセージをパラメータとして指定する方法としない方法です。 オプションで指定したメッセージは、通常はテストが失敗したことが報告される場面で 表示されます。これにより、デバッグが楽になります。
例 21.2: メッセージつきのアサーション
<?php
require_once 'PHPUnit/Framework.php';
class MessageTest extends PHPUnit_Framework_TestCase
{
public function testMessage()
{
$this->assertTrue(FALSE, 'これは独自のメッセージです。');
}
}
?>
以下の例は、 例 21.2 のテスト testMessage() でメッセージつきのアサーションを使用した場合の出力結果です。
phpunit MessageTest
PHPUnit 3.2.10 by Sebastian Bergmann.
F
Time: 0 seconds
There was 1 failure:
1) testMessage(MessageTest)
これは独自のメッセージです。
Failed asserting that <boolean:false> is true.
/home/sb/MessageTest.php:8
FAILURES!
Tests: 1, Failures: 1.
表 21.1 に、すべてのアサーションをまとめます。
表21.1 アサーション
| アサーション | 意味 |
|---|---|
void assertArrayHasKey(mixed $key, array $array)
|
$array にキー $key が存在しない場合にエラーを報告します。
|
void assertArrayHasKey(mixed $key, array $array, string $message)
|
$array にキー $key が存在しない場合にエラー $message を報告します。
|
void assertClassHasAttribute(string $attributeName, string $className)
|
$className::attributeName が存在しない場合にエラーを報告します。
|
void assertClassHasAttribute(string $attributeName, string $className, string $message)
|
$className::attributeName が存在しない場合にエラー $message を報告します。
|
void assertClassHasStaticAttribute(string $attributeName, string $className)
|
$className::attributeName が存在しないか、あるいは static でない場合にエラーを報告します。
|
void assertClassHasStaticAttribute(string $attributeName, string $className, string $message)
|
$className::attributeName が存在しないか、あるいは static でない場合にエラー $message を報告します。
|
void assertContains(mixed $needle, array $haystack)
|
$needle が $haystack の要素でない場合にエラーを報告します。
|
void assertContains(mixed $needle, array $haystack, string $message)
|
$needle が $haystack の要素でない場合にエラー $message を報告します。
|
void assertContains(mixed $needle, Iterator $haystack)
|
$needle が $haystack の要素でない場合にエラーを報告します。
|
void assertContains(mixed $needle, Iterator $haystack, string $message)
|
$needle が $haystack の要素でない場合にエラー $message を報告します。
|
void assertContains(string $needle, string $haystack)
|
$needle が $haystack の一部でない場合にエラーを報告します。
|
void assertContains(string $needle, string $haystack, string $message)
|
$needle が $haystack の一部でない場合にエラー $message を報告します。
|
assertContainsOnly(string $type, array $haystack)
|
$haystack の中身の型が $type だけではない場合にエラーを報告します。
|
assertContainsOnly(string $type, array $haystack, NULL, string $message)
|
$haystack の中身の型が $type だけではない場合にエラー $message を報告します。
|
assertContainsOnly(string $type, array $haystack, bool $isNativeType)
|
$haystack の中身の変数の型が $type だけではない場合にエラーを報告します。$isNativeType はフラグで、$type がネイティブな PHP の型であるかどうかを表します。
|
assertContainsOnly(string $type, array $haystack, bool $isNativeType, string $message)
|
$haystack の中身の変数の型が $type だけではない場合にエラー $message を報告します。$isNativeType はフラグで、$type がネイティブな PHP の型であるかどうかを表します。
|
assertContainsOnly(string $type, Iterator $haystack)
|
$haystack の中身の変数の型が $type だけではない場合にエラーを報告します。
|
assertContainsOnly(string $type, Iterator $haystack, NULL, string $message)
|
$haystack の中身の変数の型が $type だけではない場合にエラー $message を報告します。
|
assertContainsOnly(string $type, Iterator $haystack, bool $isNativeType)
|
$haystack の中身の変数の型が $type だけではない場合にエラーを報告します。$isNativeType はフラグで、$type がネイティブな PHP の型であるかどうかを表します。
|
assertContainsOnly(string $type, Iterator $haystack, bool $isNativeType, string $message)
|
$haystack の中身の変数の型が $type だけではない場合にエラー $message を報告します。$isNativeType はフラグで、$type がネイティブな PHP の型であるかどうかを表します。
|
void assertEquals(array $expected, array $actual)
|
2 つの配列 $expected と $actual が等しくない場合にエラーを報告します。
|
void assertEquals(array $expected, array $actual, string $message)
|
2 つの配列 $expected と $actual が等しくない場合にエラー $message を報告します。
|
void assertEquals(float $expected, float $actual, '', float $delta = 0)
|
2 つの float 値 $expected と $actual の誤差が $delta より大きい場合にエラーを報告します。
|
void assertEquals(float $expected, float $actual, string $message, float $delta = 0)
|
2 つの float 値 $expected と $actual の誤差が $delta より大きい場合にエラー $message を報告します。
|
void assertEquals(object $expected, object $actual)
|
2 つのオブジェクト $expected と $actual が同じ属性値を持たない場合にエラーを報告します。
|
void assertEquals(object $expected, object $actual, string $message)
|
2 つのオブジェクト $expected と $actual が同じ属性値を持たない場合にエラー $message を報告します。
|
void assertEquals(string $expected, string $actual)
|
2 つの文字列 $expected と $actual が等しくない場合にエラーを報告します。エラーは、2 つの文字列の差分で報告されます。
|
void assertEquals(string $expected, string $actual, string $message)
|
2 つの文字列 $expected と $actual が等しくない場合にエラー $message を報告します。エラーは、2 つの文字列の差分で報告されます。
|
void assertEquals(DOMDocument $expected, DOMDocument $actual)
|
2 つの DOMDocument オブジェクト $expected と $actual で表される XML ドキュメントが (コメントを除去して正規化した状態で) 等しくない場合にエラーを報告します。
|
void assertEquals(DOMDocument $expected, DOMDocument $actual, string $message)
|
2 つの DOMDocument オブジェクト $expected と $actual で表される XML ドキュメントが (コメントを除去して正規化した状態で) 等しくない場合にエラー $message を報告します。
|
void assertEquals(mixed $expected, mixed $actual)
|
2 つの変数 $expected と $actual が等しくない場合にエラーを報告します。
|
void assertEquals(mixed $expected, mixed $actual, string $message)
|
2 つの変数 $expected と $actual が等しくない場合にエラー $message を報告します。
|
void assertFalse(bool $condition)
|
$condition が TRUE の場合にエラーを報告します。
|
void assertFalse(bool $condition, string $message)
|
$condition が TRUE の場合にエラー $message を報告します。
|
void assertFileEquals(string $expected, string $actual)
|
$expected で指定したファイルと $actual で指定したファイルの内容が異なる場合にエラーを報告します。
|
void assertFileEquals(string $expected, string $actual, string $message)
|
$expected で指定したファイルと $actual で指定したファイルの内容が異なる場合にエラー $message を報告します。
|
void assertFileExists(string $filename)
|
ファイル $filename が存在しない場合にエラーを報告します。
|
void assertFileExists(string $filename, string $message)
|
ファイル $filename が存在しない場合にエラー $message を報告します。
|
void assertGreaterThan(mixed $expected, mixed $actual)
|
$actual の値が $expected の値より大きくない場合にエラーを報告します。
|
void assertGreaterThan(mixed $expected, mixed $actual, string $message)
|
$actual の値が $expected の値より大きくない場合にエラー $message を報告します。
|
void assertGreaterThanOrEqual(mixed $expected, mixed $actual)
|
$actual の値が $expected の値以上でない場合にエラーを報告します。
|
void assertGreaterThanOrEqual(mixed $expected, mixed $actual, string $message)
|
$actual の値が $expected の値以上でない場合にエラー $message を報告します。
|
void assertLessThan(mixed $expected, mixed $actual)
|
$actual の値が $expected の値より小さくない場合にエラーを報告します。
|
void assertLessThan(mixed $expected, mixed $actual, string $message)
|
$actual の値が $expected の値より小さくない場合にエラー $message を報告します。
|
void assertLessThanOrEqual(mixed $expected, mixed $actual)
|
$actual の値が $expected の値以下でない場合にエラーを報告します。
|
void assertLessThanOrEqual(mixed $expected, mixed $actual, string $message)
|
$actual の値が $expected の値以下でない場合にエラー $message を報告します。
|
void assertNull(mixed $variable)
|
$variable が NULL でないときにエラーを報告します。
|
void assertNull(mixed $variable, string $message)
|
$variable が NULL でないときにエラー $message を報告します。
|
void assertObjectHasAttribute(string $attributeName, object $object)
|
$object->attributeName が存在しない場合にエラーを報告します。
|
void assertObjectHasAttribute(string $attributeName, object $object, string $message)
|
$object->attributeName が存在しない場合にエラー $message を報告します。
|
void assertRegExp(string $pattern, string $string)
|
$string が正規表現 $pattern にマッチしない場合にエラーを報告します。
|
void assertRegExp(string $pattern, string $string, string $message)
|
$string が正規表現 $pattern にマッチしない場合にエラー $message を報告します。
|
void assertSame(object $expected, object $actual)
|
2 つの変数 $expected と $actual が同じオブジェクトを参照していない場合にエラーを報告します。
|
void assertSame(object $expected, object $actual, string $message)
|
2 つの変数 $expected と $actual が同じオブジェクトを参照していない場合にエラー $message を報告します。
|
void assertSame(mixed $expected, mixed $actual)
|
2 つの変数 $expected と $actual が同じ型・同じ値でない場合にエラーを報告します。
|
void assertSame(mixed $expected, mixed $actual, string $message)
|
2 つの変数 $expected と $actual が同じ型・同じ値でない場合にエラー $message を報告します。
|
void assertTrue(bool $condition)
|
$condition が FALSE の場合にエラーを報告します。
|
void assertTrue(bool $condition, string $message)
|
$condition が FALSE の場合にエラー $message を報告します。
|
void assertType(string $expected, mixed $actual)
|
変数 $actual の型が $expected でない場合にエラーを報告します。
|
void assertType(string $expected, mixed $actual, string $message)
|
変数 $actual の型が $expected でない場合にエラー $messageを報告します。
|
void assertXmlFileEqualsXmlFile(string $expectedFile, string $actualFile)
|
ファイル $actualFile の XML ドキュメントがファイル $expectedFile の XML ドキュメントと等しくない場合にエラーを報告します。
|
void assertXmlFileEqualsXmlFile(string $expectedFile, string $actualFile, string $message)
|
ファイル $actualFile の XML ドキュメントがファイル $expectedFile の XML ドキュメントと等しくない場合にエラー $message を報告します。
|
void assertXmlStringEqualsXmlString(string $expectedXml, string $actualXml)
|
$actualXml の XML ドキュメントが $expectedXml の XML ドキュメントと等しくない場合にエラーを報告します。
|
void assertXmlStringEqualsXmlString(string $expectedXml, string $actualXml, string $message)
|
$actualXml の XML ドキュメントが $expectedXml の XML ドキュメントと等しくない場合にエラー $message を報告します。
|
void assertArrayNotHasKey(mixed $key, array $array)
|
$array にキー $key が存在する場合にエラーを報告します。
|
void assertArrayNotHasKey(mixed $key, array $array, string $message)
|
$array にキー $key が存在する場合にエラー $message を報告します。
|
void assertClassNotHasAttribute(string $attributeName, string $className)
|
$className::attributeName が存在する場合にエラーを報告します。
|
void assertClassNotHasAttribute(string $attributeName, string $className, string $message)
|
$className::attributeName が存在する場合にエラー $message を報告します。
|
void assertClassNotHasStaticAttribute(string $attributeName, string $className)
|
$className::attributeName が存在し、かつ static である場合にエラーを報告します。
|
void assertClassNotHasStaticAttribute(string $attributeName, string $className, string $message)
|
$className::attributeName が存在し、かつ static である場合にエラー $message を報告します。
|
void assertNotContains(mixed $needle, array $haystack)
|
$needle が $haystack の要素である場合にエラーを報告します。
|
void assertNotContains(mixed $needle, array $haystack, string $message)
|
$needle が $haystack の要素である場合にエラー $message を報告します。
|
void assertNotContains(mixed $needle, Iterator $haystack)
|
$needle が $haystack の要素である場合にエラーを報告します。
|
void assertNotContains(mixed $needle, Iterator $haystack, string $message)
|
$needle が $haystack の要素である場合にエラー $message を報告します。
|
void assertNotContains(string $needle, string $haystack)
|
$needle が $haystack の一部である場合にエラーを報告します。
|
void assertNotContains(string $needle, string $haystack, string $message)
|
$needle が $haystack の一部である場合にエラー $message を報告します。
|
assertNotContainsOnly(string $type, array $haystack)
|
$haystack の中身の型が $type だけである場合にエラーを報告します。
|
assertNotContainsOnly(string $type, array $haystack, NULL, string $message)
|
$haystack の中身の型が $type だけである場合にエラー $message を報告します。
|
assertNotContainsOnly(string $type, array $haystack, bool $isNativeType)
|
$haystack の中身の変数の型が $type だけである場合にエラーを報告します。$isNativeType はフラグで、$type がネイティブな PHP の型であるかどうかを表します。
|
assertNotContainsOnly(string $type, array $haystack, bool $isNativeType, string $message)
|
$haystack の中身の変数の型が $type だけである場合にエラー $message を報告します。$isNativeType はフラグで、$type がネイティブな PHP の型であるかどうかを表します。
|
assertNotContainsOnly(string $type, Iterator $haystack)
|
$haystack の中身の変数の型が $type だけである場合にエラーを報告します。
|
assertNotContainsOnly(string $type, Iterator $haystack, NULL, string $message)
|
$haystack の中身の変数の型が $type だけである場合にエラー $message を報告します。
|
assertNotContainsOnly(string $type, Iterator $haystack, bool $isNativeType)
|
$haystack の中身の変数の型が $type だけである場合にエラーを報告します。$isNativeType はフラグで、$type がネイティブな PHP の型であるかどうかを表します。
|
assertNotContainsOnly(string $type, Iterator $haystack, bool $isNativeType, string $message)
|
$haystack の中身の変数の型が $type だけである場合にエラー $message を報告します。$isNativeType はフラグで、$type がネイティブな PHP の型であるかどうかを表します。
|
void assertNotEquals(array $expected, array $actual)
|
2 つの配列 $expected と $actual が等しい場合にエラーを報告します。
|
void assertNotEquals(array $expected, array $actual, string $message)
|
2 つの配列 $expected と $actual が等しい場合にエラー $message を報告します。
|
void assertNotEquals(float $expected, float $actual, '', float $delta = 0)
|
2 つの float 値 $expected と $actual の誤差が $delta 以下の場合にエラーを報告します。
|
void assertNotEquals(float $expected, float $actual, string $message, float $delta = 0)
|
2 つの float 値 $expected と $actual の誤差が $delta 以下の場合にエラー $message を報告します。
|
void assertNotEquals(object $expected, object $actual)
|
2 つのオブジェクト $expected と $actual が同じ属性値を持つ場合にエラーを報告します。
|
void assertNotEquals(object $expected, object $actual, string $message)
|
2 つのオブジェクト $expected と $actual が同じ属性値を持つ場合にエラー $message を報告します。
|
void assertNotEquals(string $expected, string $actual)
|
2 つの文字列 $expected と $actual が等しい場合にエラーを報告します。
|
void assertNotEquals(string $expected, string $actual, string $message)
|
2 つの文字列 $expected と $actual が等しい場合にエラー $message を報告します。
|
void assertNotEquals(DOMDocument $expected, DOMDocument $actual)
|
2 つの DOMDocument オブジェクト $expected と $actual で表される XML ドキュメントが (コメントを除去して正規化した状態で) 等しい場合にエラーを報告します。
|
void assertNotEquals(DOMDocument $expected, DOMDocument $actual, string $message)
|
2 つの DOMDocument オブジェクト $expected と $actual で表される XML ドキュメントが (コメントを除去して正規化した状態で) 等しい場合にエラー $message を報告します。
|
void assertNotEquals(mixed $expected, mixed $actual)
|
2 つの変数 $expected と $actual が等しい場合にエラーを報告します。
|
void assertNotEquals(mixed $expected, mixed $actual, string $message)
|
2 つの変数 $expected と $actual が等しい場合にエラー $message を報告します。
|
void assertFileNotEquals(string $expected, string $actual)
|
$expected で指定したファイルと $actual で指定したファイルの内容が同じ場合にエラーを報告します。
|
void assertFileNotEquals(string $expected, string $actual, string $message)
|
$expected で指定したファイルと $actual で指定したファイルの内容が同じ場合にエラー $message を報告します。
|
void assertFileNotExists(string $filename)
|
ファイル $filename が存在する場合にエラーを報告します。
|
void assertFileNotExists(string $filename, string $message)
|
ファイル $filename が存在する場合にエラー $message を報告します。
|
void assertNotNull(mixed $variable)
|
$variable が NULL の場合にエラーを報告します。
|
void assertNotNull(mixed $variable, string $message)
|
$variable が NULL の場合にエラー $message を報告します。
|
void assertNotRegExp(string $pattern, string $string)
|
$string が正規表現 $pattern にマッチする場合にエラーを報告します。
|
void assertNotRegExp(string $pattern, string $string, string $message)
|
$string が正規表現 $pattern にマッチする場合にエラー $message を報告します。
|
void assertNotSame(object $expected, object $actual)
|
2 つの変数 $expected と $actual が同じオブジェクトを参照している場合にエラーを報告します。
|
void assertNotSame(object $expected, object $actual, string $message)
|
2 つの変数 $expected と $actual が同じオブジェクトを参照している場合にエラー $message を報告します。
|
void assertNotSame(mixed $expected, mixed $actual)
|
2 つの変数 $expected と $actual が同じ型・同じ値である場合にエラーを報告します。
|
void assertNotSame(mixed $expected, mixed $actual, string $message)
|
2 つの変数 $expected と $actual が同じ型・同じ値である場合にエラー $message を報告します。
|
void assertNotType(string $expected, mixed $actual)
|
変数 $actual の型が $expected である場合にエラーを報告します。
|
void assertNotType(string $expected, mixed $actual, string $message)
|
変数 $actual の型が $expected である場合にエラー $messageを報告します。
|
void assertObjectNotHasAttribute(string $attributeName, object $object)
|
$object->attributeName が存在する場合にエラーを報告します。
|
void assertObjectNotHasAttribute(string $attributeName, object $object, string $message)
|
$object->attributeName が存在する場合にエラー $message を報告します。
|
void assertXmlFileNotEqualsXmlFile(string $expectedFile, string $actualFile)
|
ファイル $actualFile の XML ドキュメントがファイル $expectedFile の XML ドキュメントと等しい場合にエラーを報告します。
|
void assertXmlFileNotEqualsXmlFile(string $expectedFile, string $actualFile, string $message)
|
ファイル $actualFile の XML ドキュメントがファイル $expectedFile の XML ドキュメントと等しい場合にエラー $message を報告します。
|
void assertXmlStringNotEqualsXmlString(string $expectedXml, string $actualXml)
|
$actualXml の XML ドキュメントが $expectedXml の XML ドキュメントと等しい場合にエラーを報告します。
|
void assertXmlStringNotEqualsXmlString(string $expectedXml, string $actualXml, string $message)
|
$actualXml の XML ドキュメントが $expectedXml の XML ドキュメントと等しい場合にエラー $message を報告します。
|
void assertAttributeContains(mixed $needle, string $haystackAttributeName, string $haystackClassName)
|
$needle が $haystackClassName::$haystackAttributeName の要素でない場合にエラーを報告します。$haystackClassName::$haystackAttributeName は、配列か文字列、あるいは Iterator インターフェイスを実装したオブジェクトです。$haystackClassName::$haystackAttributeName 属性は public、protected あるいは private のいずれかとなります。
|
void assertAttributeContains(mixed $needle, string $haystackAttributeName, string $haystackClassName, string $message)
|
$needle が $haystackClassName::$haystackAttributeName の要素でない場合にエラー $message を報告します。$haystackClassName::$haystackAttributeName は、配列か文字列、あるいは Iterator インターフェイスを実装したオブジェクトです。$haystackClassName::$haystackAttributeName 属性は public、protected あるいは private のいずれかとなります。
|
void assertAttributeNotContains(mixed $needle, string $haystackAttributeName, string $haystackClassName)
|
$needle が $haystackClassName::$haystackAttributeName の要素である場合にエラーを報告します。$haystackClassName::$haystackAttributeName は、配列か文字列、あるいはIterator インターフェイスを実装したオブジェクトです。$haystackClassName::$haystackAttributeName 属性は public、protected あるいは private のいずれかとなります。
|
void assertAttributeNotContains(mixed $needle, string $haystackAttributeName, string $haystackClassName, string $message)
|
$needle が $haystackClassName::$haystackAttributeName の要素である場合にエラー $message を報告します。$haystackClassName::$haystackAttributeName は、配列か文字列、あるいはIterator インターフェイスを実装したオブジェクトです。$haystackClassName::$haystackAttributeName 属性は public、protected あるいは private のいずれかとなります。
|
void assertAttributeContains(mixed $needle, string $haystackAttributeName, object $haystackObject)
|
$needle が $haystackObject->haystackAttributeName の要素でない場合にエラーを報告します。$haystackObject->haystackAttributeName は、配列か文字列、あるいは Iterator インターフェイスを実装したオブジェクトです。$haystackObject->haystackAttributeName 属性は public、protected あるいは private のいずれかとなります。
|
void assertAttributeContains(mixed $needle, string $haystackAttributeName, object $haystackObject, string $message)
|
$needle が $haystackObject->haystackAttributeName の要素でない場合にエラー $message を報告します。$haystackObject->haystackAttributeName は、配列か文字列、あるいは Iterator インターフェイスを実装したオブジェクトです。$haystackObject->haystackAttributeName 属性は public、protected あるいは private のいずれかとなります。
|
void assertAttributeNotContains(mixed $needle, string $haystackAttributeName, object $haystackObject)
|
$needle が $haystackObject->haystackAttributeName の要素である場合にエラーを報告します。$haystackObject->haystackAttributeName は、配列か文字列、あるいは Iterator インターフェースを実装したオブジェクトです。$haystackObject->haystackAttributeName 属性は public、protected あるいは private のいずれかとなります。
|
void assertAttributeNotContains(mixed $needle, string $haystackAttributeName, object $haystackObject, string $message)
|
$needle が $haystackObject->haystackAttributeName の要素である場合にエラー $message を報告します。$haystackObject->haystackAttributeName は、配列か文字列、あるいは Iterator インターフェースを実装したオブジェクトです。$haystackObject->haystackAttributeName 属性は public、protected あるいは private のいずれかとなります。
|
void assertAttributeEquals(array $expected, string $actualAttributeName, string $actualClassName)
|
2 つの配列 $expected と $actualClassName::$actualAttributeName が等しくない場合にエラーを報告します。$actualClassName::$actualAttributeName 属性は public、protected あるいは private のいずれかとなります。
|
void assertAttributeEquals(array $expected, string $actualAttributeName, string $actualClassName, string $message)
|
2 つの配列 $expected と $actualClassName::$actualAttributeName が等しくない場合にエラー $message を報告します。$actualClassName::$actualAttributeName 属性は public、protected あるいは private のいずれかとなります。
|
void assertAttributeNotEquals(array $expected, string $actualAttributeName, string $actualClassName)
|
2 つの配列 $expected と $actualClassName::$actualAttributeName が等しい場合にエラーを報告します。$actualClassName::$actualAttributeName 属性は public、protected あるいは private のいずれかとなります。
|
void assertAttributeNotEquals(array $expected, string $actualAttributeName, string $actualClassName, string $message)
|
2 つの配列 $expected と $actualClassName::$actualAttributeName が等しい場合にエラー $message を報告します。$actualClassName::$actualAttributeName 属性は public、protected あるいは private のいずれかとなります。
|
void assertAttributeEquals(float $expected, string $actualAttributeName, string $actualClassName, '', float $delta = 0)
|
2 つの float 値 $expected と $actualClassName::$actualAttributeName の誤差が $delta より大きい場合にエラーを報告します。$actualClassName::$actualAttributeName 属性は public、protected あるいは private のいずれかとなります。
|
void assertAttributeEquals(float $expected, string $actualAttributeName, string $actualClassName, string $message, float $delta = 0)
|
2 つの float 値 $expected と $actualClassName::$actualAttributeName の誤差が $delta より大きい場合にエラー $message を報告します。$actualClassName::$actualAttributeName 属性は public、protected あるいは private のいずれかとなります。
|
void assertAttributeNotEquals(float $expected, string $actualAttributeName, string $actualClassName, '', float $delta = 0)
|
2 つの float 値 $expected と $actualClassName::$actualAttributeName の誤差が $delta 以内である場合にエラーを報告します。$actualClassName::$actualAttributeName 属性は public、protected あるいは private のいずれかとなります。
|
void assertAttributeNotEquals(float $expected, string $actualAttributeName, string $actualClassName, string $message, float $delta = 0)
|
2 つの float 値 $expected と $actualClassName::$actualAttributeName の誤差が $delta 以内である場合にエラー $message を報告します。$actualClassName::$actualAttributeName 属性は public、protected あるいは private のいずれかとなります。
|
void assertAttributeEquals(string $expected, string $actualAttributeName, string $actualClassName)
|
2 つの文字列 $expected と $actualClassName::$actualAttributeName が等しくない場合にエラーを報告します。エラーは、2 つの文字列の差分で報告されます。$actualClassName::$actualAttributeName 属性は public、protected あるいは private のいずれかとなります。
|
void assertAttributeEquals(string $expected, string $actualAttributeName, string $actualClassName, string $message)
|
2 つの文字列 $expected と $actualClassName::$actualAttributeName が等しくない場合にエラー $message を報告します。エラーは、2 つの文字列の差分で報告されます。$actualClassName::$actualAttributeName 属性は public、protected あるいは private のいずれかとなります。
|
void assertAttributeNotEquals(string $expected, string $actualAttributeName, string $actualClassName)
|
2 つの文字列 $expected と $actualClassName::$actualAttributeName が等しい場合にエラーを報告します。$actualClassName::$actualAttributeName 属性は public、protected あるいは private のいずれかとなります。
|
void assertAttributeNotEquals(string $expected, string $actualAttributeName, string $actualClassName, string $message)
|
2 つの文字列 $expected と $actualClassName::$actualAttributeName が等しい場合にエラー $message を報告します。$actualClassName::$actualAttributeName 属性は public、protected あるいは private のいずれかとなります。
|
void assertAttributeEquals(mixed $expected, string $actualAttributeName, string $actualClassName)
|
2 つの変数 $expected と $actualClassName::$actualAttributeName が等しくない場合にエラーを報告します。$actualClassName::$actualAttributeName 属性は public、protected あるいは private のいずれかとなります。
|
void assertAttributeEquals(mixed $expected, string $actualAttributeName, string $actualClassName, string $message)
|
2 つの変数 $expected と $actualClassName::$actualAttributeName が等しくない場合にエラー $message を報告します。$actualClassName::$actualAttributeName 属性は public、protected あるいは private のいずれかとなります。
|
void assertAttributeNotEquals(mixed $expected, string $actualAttributeName, string $actualClassName)
|
2 つの変数 $expected と $actualClassName::$actualAttributeName が等しい場合にエラーを報告します。$actualClassName::$actualAttributeName 属性は public、protected あるいは private のいずれかとなります。
|
void assertAttributeNotEquals(mixed $expected, string $actualAttributeName, string $actualClassName, string $message)
|
2 つの変数 $expected と $actualClassName::$actualAttributeName が等しい場合にエラー $message を報告します。$actualClassName::$actualAttributeName 属性は public、protected あるいは private のいずれかとなります。
|
void assertAttributeEquals(array $expected, string $actualAttributeName, object $actualObject)
|
2 つの配列 $expected と $actualObject->actualAttributeName が等しくない場合にエラーを報告します。$actualObject->actualAttributeName 属性は public、protected あるいは private のいずれかとなります。
|
void assertAttributeEquals(array $expected, string $actualAttributeName, object $actualObject, string $message)
|
2 つの配列 $expected と $actualObject->actualAttributeName が等しくない場合にエラーを $message 報告します。$actualObject->actualAttributeName 属性は public、protected あるいは private のいずれかとなります。
|
void assertAttributeNotEquals(array $expected, string $actualAttributeName, object $actualObject)
|
2 つの配列 $expected と $actualObject->actualAttributeName が等しい場合にエラーを報告します。$actualObject->actualAttributeName 属性は public、protected あるいは private のいずれかとなります。
|
void assertAttributeNotEquals(array $expected, string $actualAttributeName, object $actualObject, string $message)
|
2 つの配列 $expected と $actualObject->actualAttributeName が等しい場合にエラーを $message 報告します。$actualObject->actualAttributeName 属性は public、protected あるいは private のいずれかとなります。
|
void assertAttributeEquals(float $expected, string $actualAttributeName, object $actualObject, '', float $delta = 0)
|
2 つの float 値 $expected と $actualObject->actualAttributeName の誤差が $delta より大きい場合にエラーを報告します。$actualObject->actualAttributeName 属性は public、protected あるいは private のいずれかとなります。
|
void assertAttributeEquals(float $expected, string $actualAttributeName, object $actualObject, string $message, float $delta = 0)
|
2 つの float 値 $expected と $actualObject->actualAttributeName の誤差が $delta より大きい場合にエラー $message を報告します。$actualObject->actualAttributeName 属性は public、protected あるいは private のいずれかとなります。
|
void assertAttributeNotEquals(float $expected, string $actualAttributeName, object $actualObject, '', float $delta = 0)
|
2 つの float 値 $expected と $actualObject->actualAttributeName の誤差が $delta 以下の場合にエラーを報告します。$actualObject->actualAttributeName 属性は public、protected あるいは private のいずれかとなります。
|
void assertAttributeNotEquals(float $expected, string $actualAttributeName, object $actualObject, string $message, float $delta = 0)
|
2 つの float 値 $expected と $actualObject->actualAttributeName の誤差が $delta 以下の場合にエラー $message を報告します。$actualObject->actualAttributeName 属性は public、protected あるいは private のいずれかとなります。
|
void assertAttributeEquals(string $expected, string $actualAttributeName, object $actualObject)
|
2 つの文字列 $expected と $actualObject->actualAttributeName が等しくない場合にエラーを報告します。エラーは、2 つの文字列の差分で報告されます。$actualObject->actualAttributeName 属性は public、protected あるいは private のいずれかとなります。
|
void assertAttributeEquals(string $expected, string $actualAttributeName, object $actualObject, string $message)
|
2 つの文字列 $expected と $actualObject->actualAttributeName が等しくない場合にエラー $message を報告します。エラーは、2 つの文字列の差分で報告されます。$actualObject->actualAttributeName 属性は public、protected あるいは private のいずれかとなります。
|
void assertAttributeNotEquals(string $expected, string $actualAttributeName, object $actualObject)
|
2 つの文字列 $expected と $actualObject->actualAttributeName が等しい場合にエラーを報告します。$actualObject->actualAttributeName 属性は public、protected あるいは private のいずれかとなります。
|
void assertAttributeNotEquals(string $expected, string $actualAttributeName, object $actualObject, string $message)
|
2 つの文字列 $expected と $actualObject->actualAttributeName が等しい場合にエラー $message を報告します。$actualObject->actualAttributeName 属性は public、protected あるいは private のいずれかとなります。
|
void assertAttributeEquals(mixed $expected, string $actualAttributeName, object $actualObject)
|
2 つの変数 $expected と $actualObject->actualAttributeName が等しくない場合にエラーを報告します。$actualObject->actualAttributeName 属性は public、protected あるいは private のいずれかとなります。
|
void assertAttributeEquals(mixed $expected, string $actualAttributeName, object $actualObject, string $message)
|
2 つの変数 $expected と $actualObject->actualAttributeName が等しくない場合にエラー $message を報告します。$actualObject->actualAttributeName 属性は public、protected あるいは private のいずれかとなります。
|
void assertAttributeNotEquals(mixed $expected, string $actualAttributeName, object $actualObject)
|
2 つの変数 $expected と $actualObject->actualAttributeName が等しい場合にエラーを報告します。$actualObject->actualAttributeName 属性は public、protected あるいは private のいずれかとなります。
|
void assertAttributeNotEquals(mixed $expected, string $actualAttributeName, object $actualObject, string $message)
|
2 つの変数 $expected と $actualObject->actualAttributeName が等しい場合にエラー $message を報告します。$actualObject->actualAttributeName 属性は public、protected あるいは private のいずれかとなります。
|
void assertAttributeSame(object $expected, string $actualAttributeName, string $actualClassName)
|
$actualClassName::$actualAttributeName と $actual が同じオブジェクトを参照していない場合にエラーを報告します。$actualClassName::$actualAttributeName 属性は public、protected あるいは private のいずれかとなります。
|
void assertAttributeSame(object $expected, string $actualAttributeName, string $actualClassName, string $message)
|
$actualClassName::$actualAttributeName と $actual が同じオブジェクトを参照していない場合にエラー $message を報告します。$actualClassName::$actualAttributeName 属性は public、protected あるいは private のいずれかとなります。
|
void assertAttributeSame(mixed $expected, string $actualAttributeName, string $actualClassName)
|
$actualClassName::$actualAttributeName と $actual が同じ型・同じ値でない場合にエラーを報告します。$actualClassName::$actualAttributeName 属性は public、protected あるいは private のいずれかとなります。
|
void assertAttributeSame(mixed $expected, string $actualAttributeName, string $actualClassName, string $message)
|
$actualClassName::$actualAttributeName と $actual が同じ型・同じ値でない場合にエラー $message を報告します。$actualClassName::$actualAttributeName 属性は public、protected あるいは private のいずれかとなります。
|
void assertAttributeNotSame(object $expected, string $actualAttributeName, string $actualClassName)
|
$actualClassName::$actualAttributeName と $actual が同じオブジェクトを参照している場合にエラーを報告します。$actualClassName::$actualAttributeName 属性は public、protected あるいは private のいずれかとなります。
|
void assertAttributeNotSame(object $expected, string $actualAttributeName, string $actualClassName, string $message)
|
$actualClassName::$actualAttributeName と $actual が同じオブジェクトを参照している場合にエラー $message を報告します。$actualClassName::$actualAttributeName 属性は public、protected あるいは private のいずれかとなります。
|
void assertAttributeNotSame(mixed $expected, string $actualAttributeName, string $actualClassName)
|
$actualClassName::$actualAttributeName と $actual が同じ型・同じ値である場合にエラーを報告します。$actualClassName::$actualAttributeName 属性は public、protected あるいは private のいずれかとなります。
|
void assertAttributeNotSame(mixed $expected, string $actualAttributeName, string $actualClassName, string $message)
|
$actualClassName::$actualAttributeName と $actual が同じ型・同じ値である場合にエラー $message を報告します。$actualClassName::$actualAttributeName 属性は public、protected あるいは private のいずれかとなります。
|
void assertAttributeSame(object $expected, string $actualAttributeName, object $actualObject)
|
$actualObject->actualAttributeName と $actual が同じオブジェクトを参照していない場合にエラーを報告します。 $actualObject->actualAttributeName 属性は public、protected あるいは private のいずれかとなります。
|
void assertAttributeSame(object $expected, string $actualAttributeName, object $actualObject, string $message)
|
$actualObject->actualAttributeName と $actual が同じオブジェクトを参照していない場合にエラー $message を報告します。 $actualObject->actualAttributeName 属性は public、protected あるいは private のいずれかとなります。
|
void assertAttributeSame(mixed $expected, string $actualAttributeName, object $actualObject)
|
$actualObject->actualAttributeName と $actual が同じ型、同じ値でない場合にエラーを報告します。$actualObject->actualAttributeName 属性は public、protected あるいは private のいずれかとなります。
|
void assertAttributeSame(mixed $expected, string $actualAttributeName, object $actualObject, string $message)
|
$actualObject->actualAttributeName と $actual が同じ型、同じ値でない場合にエラー $message を報告します。$actualObject->actualAttributeName 属性は public、protected あるいは private のいずれかとなります。
|
void assertAttributeNotSame(object $expected, string $actualAttributeName, object $actualObject)
|
$actualObject->actualAttributeName と $actual が同じオブジェクトを参照している場合にエラーを報告します。 $actualObject->actualAttributeName 属性は public、protected あるいは private のいずれかとなります。
|
void assertAttributeNotSame(object $expected, string $actualAttributeName, object $actualObject, string $message)
|
$actualObject->actualAttributeName と $actual が同じオブジェクトを参照している場合にエラー $message を報告します。 $actualObject->actualAttributeName 属性は public、protected あるいは private のいずれかとなります。
|
void assertAttributeNotSame(mixed $expected, string $actualAttributeName, object $actualObject)
|
$actualObject->actualAttributeName と $actual が同じ型、同じ値である場合にエラーを報告します。$actualObject->actualAttributeName 属性は public、protected あるいは private のいずれかとなります。
|
void assertAttributeNotSame(mixed $expected, string $actualAttributeName, object $actualObject, string $message)
|
$actualObject->actualAttributeName と $actual が同じ型、同じ値である場合にエラー $message を報告します。$actualObject->actualAttributeName 属性は public、protected あるいは private のいずれかとなります。
|
もっと複雑なアサーションを行う場合には、 PHPUnit_Framework_Constraint クラスを使用します。 これらは、assertThat() メソッドを使用して 例 21.3 のように評価されます。
例 21.3: assertThat() での制約オブジェクトの使用
<?php
require_once 'PHPUnit/Framework.php';
class ConstraintTest extends PHPUnit_Framework_TestCase
{
public function testNotEquals()
{
$constraint = $this->logicalNot(
$this->equalTo('foo')
);
$this->assertThat('foo', $constraint);
}
}
?>
phpunit ConstraintTest
PHPUnit 3.2.10 by Sebastian Bergmann.
F
Time: 0 seconds
There was 1 failure:
1) testNotEquals(ConstraintTest)
Failed asserting that <string:foo> is not equal to <string:foo>.
/home/sb/ConstraintTest.php:12
FAILURES!
Tests: 1, Failures: 1.
表 21.2 に、 使用できる PHPUnit_Framework_Constraint の実装をまとめます。
表21.2 制約
| 制約 | 意味 |
|---|---|
PHPUnit_Framework_Constraint_Attribute attribute(PHPUnit_Framework_Constraint $constraint, $attributeName)
|
別の制約を、クラスあるいはオブジェクトの属性として適用する制約。 |
PHPUnit_Framework_Constraint_IsAnything anything()
|
あらゆる入力値を受け入れる制約。 |
PHPUnit_Framework_Constraint_ArrayHasKey arrayHasKey(mixed $key)
|
配列が指定したキーを保持していることを保証する制約。 |
PHPUnit_Framework_Constraint_TraversableContains contains(mixed $value)
|
Iterator インターフェイスを実装している array やオブジェクトが、指定した値を保持していることを保証する制約。
|
PHPUnit_Framework_Constraint_IsEqual equalTo($value, $delta = 0, $maxDepth = 10)
|
ある値が別の値と等しいかどうかを調べる制約。 |
PHPUnit_Framework_Constraint_Attribute attributeEqualTo($attributeName, $value, $delta = 0, $maxDepth = 10)
|
ある値がクラスあるいはオブジェクトの属性と等しいかどうかを調べる制約。 |
PHPUnit_Framework_Constraint_FileExists fileExists()
|
指定した名前のファイルが存在するかどうかを調べる制約。 |
PHPUnit_Framework_Constraint_GreaterThan greaterThan(mixed $value)
|
評価される値が、指定した値より大きいことを保証する制約。 |
PHPUnit_Framework_Constraint_Or greaterThanOrEqual(mixed $value)
|
評価される値が、指定した値以上であることを保証する制約。 |
PHPUnit_Framework_Constraint_ClassHasAttribute classHasAttribute(string $attributeName)
|
評価されるクラスに、指定した属性があることを保証する制約。 |
PHPUnit_Framework_Constraint_ClassHasStaticAttribute classHasStaticAttribute(string $attributeName)
|
評価されるクラスに、指定した static 属性があることを保証する制約。 |
PHPUnit_Framework_Constraint_ObjectHasAttribute hasAttribute(string $attributeName)
|
評価されるオブジェクトが、指定した属性を保持していることを保証する制約。 |
PHPUnit_Framework_Constraint_IsIdentical identicalTo(mixed $value)
|
ある値が別の値と同一であることを保証する制約。 |
PHPUnit_Framework_Constraint_IsInstanceOf isInstanceOf(string $className)
|
評価されるオブジェクトが、指定したクラスのインスタンスであることを保証する制約。 |
PHPUnit_Framework_Constraint_IsType isType(string $type)
|
評価される値が、指定した型であることを保証する制約。 |
PHPUnit_Framework_Constraint_LessThan lessThan(mixed $value)
|
評価される値が、指定した値より小さいことを保証する制約。 |
PHPUnit_Framework_Constraint_Or lessThanOrEqual(mixed $value)
|
評価される値が、指定した値以下であることを保証する制約。 |
logicalAnd()
|
論理積 (AND)。 |
logicalNot(PHPUnit_Framework_Constraint $constraint)
|
論理否定 (NOT)。 |
logicalOr()
|
論理和 (OR)。 |
logicalXor()
|
排他的論理和 (XOR)。 |
PHPUnit_Framework_Constraint_PCREMatch matchesRegularExpression(string $pattern)
|
評価される文字列が、正規表現にマッチすることを保証する制約。 |
PHPUnit_Framework_Constraint_StringContains stringContains(string $string, bool $case)
|
評価される文字列が、指定した文字列を含むことを保証する制約。 |
これら以外に、プロジェクトで使用している オブジェクト固有のアサーションが必要になることもあるでしょう。独自の Assert クラスを作成し、 そこに独自のアサーションを含めてテストに使用することができます。
アサーションに失敗すると、ボトルネックメソッド fail(string $message) がコールされ、これは PHPUnit_Framework_AssertionFailedError をスローします。 このメソッドにもパラメータなしのものがあります。テストでエラーが発生した際に、 fail() を明示的にコールします。 例外が発生することが期待されるテストなどがその例になります。 表 21.3 に、PHPUnit のボトルネックメソッドをまとめます。
markTestIncomplete() および markTestSkipped() は、テストに対して「未完了」あるいは「省略」の印をつけるために便利なメソッドです。
表21.4 テストに対して「未完了」あるいは「省略」の印をつける
| メソッド | 意味 |
|---|---|
void markTestIncomplete(string $message)
|
現在のテストに「未完了」の印をつけます。$message はオプションです。
|
void markTestSkipped(string $message)
|
現在のテストに「省略」の印をつけます。$message はオプションです。
|
単体テストとは、もともとクラスの公開インターフェイスをテストするものです。 しかし、時には非公開の属性の内容をテストしたいこともあるでしょう。 readAttribute() メソッドを使用すると、 指定したオブジェクトの属性の値を取得することができます。
表21.5 非公開属性へのアクセス
| メソッド | 意味 |
|---|---|
Mixed readAttribute($object, $attributeName)
|
オブジェクトの指定した属性 ($attributeName) の値を返します。protected あるいは private である属性についても動作します。
|
PHPUnit_Framework_Test は、 テストとして働くすべてのオブジェクトが使用する、 一般的なインターフェイスです。これを実装したオブジェクトは、 ひとつあるいは複数のテストを表すことになります。 表 21.6 に示す 2 つのメソッドが定義されています。
表21.6 実装することになるメソッド
| メソッド | 意味 |
|---|---|
int count()
|
テストの数を返します。 |
void run(PHPUnit_Framework_TestResult $result)
|
テストを実行し、結果を $result で報告します。
|
PHPUnit_Framework_Test の実装クラスとして有名なのは、 PHPUnit_Framework_TestCase および PHPUnit_Framework_TestSuite の 2 つです。 PHPUnit_Framework_Test を実装したクラスを独自に作成することも可能です。 このインターフェイスはあえて小規模に設計されているので、実装するのは簡単でしょう。
テストケースクラスは PHPUnit_Framework_TestCase クラスを継承して作成します。たいていの場合は、 テストスイートから自動的にテストを実行させることになるでしょう。 この場合、(規約により) 各テストは test* という名前のメソッドにしておかなければなりません。
PHPUnit_Framework_TestCase は PHPUnit_Framework_Test::countTestCases() を実装しており、 これは常に 1 を返します。このクラスで実装されている PHPUnit_Framework_Test::run(PHPUnit_Framework_TestResult $result) は、まず setUp() を実行し、テストメソッドを実行し、 それから tearDown() を実行し、その結果を PHPUnit_Framework_TestResult に報告します。
PHPUnit_Framework_TestCase によって実装されているメソッドを 表 21.7 にまとめます。
表21.7 TestCase
| メソッド | 意味 |
|---|---|
__construct()
|
テストケースを作成します。 |
__construct(string $name)
|
指定した名前のテストケースを作成します。この名前はテストケースを表示する際に使用されます。また、リフレクションで取得するテストメソッドの名前としても使用されます。 |
string getName()
|
テストケースの名前を返します。 |
void setName($name)
|
テストケースの名前を設定します。 |
PHPUnit_Framework_TestResult run(PHPUnit_Framework_TestResult $result)
|
テストケースを実行し、結果を $result に格納するための便利なメソッドです。
|
void runTest()
|
リフレクションによってテストメソッドを実行されたくない場合に、テストメソッドをオーバーライドします。 |
object getMock($className, [array $methods, [array $arguments, [string $mockClassName, [boolean $callOriginalConstructor, [boolean $callOriginalClone, [boolean $callAutoload]]]]]])
|
指定した $className 用のモックオブジェクト (第 11 章 を参照ください) を返します。 デフォルトでは、していしたクラスの全メソッドのモックが作成されます。 二番目の (オプションの) パラメータを指定すると、 その配列の要素と一致する名前のメソッドについてのみモックが作成されます。 三番目の (オプションの) パラメータには、 モックオブジェクトのコンストラクタに渡すパラメータを配列で指定します。 四番目の (オプションの) パラメータを使用すると、 モックオブジェクトのクラス名を指定することができます。 五番目の (オプションの) パラメータを使用すると、 元のオブジェクトの __construct() メソッドをコールしないようにすることができます。 六番目の (オプションの) パラメータを使用すると、 元のオブジェクトの __clone() メソッドをコールしないようにすることができます。 七番目の (オプションの) パラメータを使用すると、 モックオブジェクトの作成時に __autoload() を無効にすることができます。
|
void iniSet(string $varName, mixed $newValue)
|
このメソッドは ini_set() 関数のラッパーです。テストが終了すると、php.ini の設定を自動的にもとの値に戻します。
|
void setLocale(integer $category, string $locale, ...)
|
このメソッドは setlocale() 関数のラッパーです。テストが終了すると、自動的にもとの設定値に戻します。
|
このクラスには、ふたつのテンプレートメソッド setUp() および tearDown() が存在します。これをオーバーライドすると、 実行しようとしているテストに関する前処理や後始末を行うことができます。 表 21.8 にこれらのメソッドをまとめます。 テンプレートメソッド assertPreConditions() および assertPostConditions() を使用すると、 テストケースクラス内のすべてのテストで実行するアサーションを定義することができます。
表21.8 テンプレートメソッド
| メソッド | 意味 |
|---|---|
void setUp()
|
これをオーバーライドして、fixture の準備、たとえばオブジェクトグラフの作成などを行います。 |
void assertPreConditions()
|
これをオーバーライドして、テストケースクラス内のすべてのテストで共有するアサーションを実行します。このメソッドがコールされるのは、テストの実行が始まる前に setUp() がコールされた後です。 |
void assertPostConditions()
|
これをオーバーライドして、テストケースクラス内のすべてのテストで共有するアサーションを実行します。このメソッドがコールされるのは、テストの実行が終わる前に tearDown() がコールされる前です。 |
void tearDown()
|
これをオーバーライドして、fixture の後始末、たとえばオブジェクトグラフの削除などを行います。 |
PHPUnit_Framework_TestSuite は複数の PHPUnit_Framework_Test を組み合わせたものです。 簡単に言うと、このクラスには複数のテストケースが含まれており、 テストスイートを実行するとそれらの全てのテストが実行されます。 テストスイートは composite なので、テストスイートの中に別のテストスイートを含め、 さらにそのテストスイートの中には別のテストスイートが含まれており…… といったことも可能です。これにより、 いろいろなところから集めたテストをひとまとめにすることが簡単になります。
run(PHPUnit_Framework_TestResult $result) および countTestCases() の 2 つに加え、 PHPUnit_Framework_TestSuite は名前つきインスタンス、 名前なしインスタンスを作成するためのメソッドも用意しています。 PHPUnit_Framework_TestSuite のインスタンスを作成するためのメソッドを 表 21.9 に示します。
表21.9 名前つき、あるいは名前なしインスタンスの作成
| メソッド | 意味 |
|---|---|
__construct()
|
空のテストスイートを返します。 |
__construct(string $theClass)
|
test* という名前のメソッドを持つ、$theClass という名前のクラスのインスタンスを含むテストスイートを返します。$theClass という名前のクラスが存在しない場合は、$theClass という名前の空のテストスイートが返されます。
|
__construct(string $theClass, string $name)
|
test* という名前のメソッドを持つ $theClass という名前のクラスのインスタンスを含む、$name という名前のテストスイートを返します。
|
__construct(ReflectionClass $theClass)
|
test* という名前のメソッドを持つ、$theClass が指すクラスのインスタンスを含むテストスイートを返します。
|
__construct(ReflectionClass $theClass, $name)
|
test* という名前のメソッドを持つ $theClass が指すクラスのインスタンスを含む、$name という名前のテストスイートを返します。
|
string getName()
|
テストスイートの名前を返します。 |
void setName(string $name)
|
テストスイートの名前を設定します。 |
void markTestSuiteSkipped(string $message)
|
現在処理中のテストスイートを、処理をスキップするように設定します。$message はオプションです。
|
PHPUnit_Framework_TestSuite には、 PHPUnit_Framework_Test を追加したり取得したりするためのメソッドも用意されています。これを 表 21.10 にまとめます。
表21.10 テストの追加、取得
| メソッド | 意味 |
|---|---|
void addTestSuite(PHPUnit_Framework_TestSuite $suite)
|
別のテストスイートを、このテストスイートに追加します。 |
void addTestSuite(string $theClass)
|
test* という名前のテストメソッドを持つ $theClass という名前のクラスのインスタンスを含むテストスイートを、このテストスイートに追加します。
|
void addTestSuite(ReflectionClass $theClass)
|
test* という名前のテストメソッドを持つ $theClass で表されるクラスのインスタンスを含むテストスイートを、このテストスイートに追加します。
|
void addTest(PHPUnit_Framework_Test $test)
|
テストスイートに $test を追加します。
|
void addTestFile(string $filename)
|
指定したソースファイルで定義されているクラスをテストスイートに追加します。 |
void addTestFiles(array $filenames)
|
指定したソースファイルで定義されているクラスをテストスイートに追加します。 |
int testCount()
|
このテストスイートに直接登録されているテストの数を返します (再帰的には検索しません)。 |
PHPUnit_Framework_Test[] tests()
|
このテストスイートに直接登録されているテストを返します。 |
PHPUnit_Framework_Test testAt(int $index)
|
$index 番目のテストを返します。
|
例 21.4 に、テストスイートを作成して実行する方法を示します。
例 21.4: テストスイートの作成および実行
<?php
require_once 'PHPUnit/Framework.php';
require_once 'ArrayTest.php';
// ArrayTest クラスのテストを含む
// テストスイートを作成します。
$suite = new PHPUnit_Framework_TestSuite('ArrayTest');
// テストを実行します。
$suite->run();
?>
第 7 章 では、 PHPUnit_Framework_TestSuite クラスを使用して 階層化されたテストケースを組み合わせる例を示します。
PHPUnit_Framework_TestSuite クラスには、 ふたつのテンプレートメソッド setUp() および tearDown() が存在します。 これらはそれぞれ、テストスイートのテストが実行される前と後にコールされます。
表21.11 テンプレートメソッド
| メソッド | 意味 |
|---|---|
void setUp()
|
テストスイートの最初のテストを実行する前にコールされます。 |
void tearDown()
|
テストスイートの最後のテストをコールした後でコールされます。 |
これらのテストを実行している間は、実行したテストの数・失敗したテスト・ テストの所要時間などをどこかに保存しておかなければなりません。 これらの結果を収集するのが PHPUnit_Framework_TestResult です。ひとつの PHPUnit_Framework_TestResult が、 テスト全体で使いまわされます。テストの実行結果や失敗の内容は PHPUnit_Framework_TestResult に記録されていき、 実行が終了すると、PHPUnit_Framework_TestResult には全てのテストの概要が含まれるようになります。
PHPUnit_Framework_TestResult は、 テストの進行状況を知りたい他のオブジェクトから参照されることもあります。 例えば、グラフィカルなテストランナーは PHPUnit_Framework_TestResult を監視し、各テストの開始時にプログレスバーを更新するでしょう。
表 21.12 は、 PHPUnit_Framework_TestResult の API をまとめたものです。
表21.12 TestResult
| メソッド | 意味 |
|---|---|
void addError(PHPUnit_Framework_Test $test, Exception $e)
|
実行中の $test から予期せぬ $e がスローされたことを記録します。
|
void addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e)
|
実行中の $test から予期せぬ $e がスローされたことを記録します。
|
PHPUnit_Framework_TestFailure[] errors()
|
記録されたエラーを返します。 |
PHPUnit_Framework_TestFailure[] failures()
|
記録された失敗を返します。 |
PHPUnit_Framework_TestFailure[] notImplemented()
|
記録された未完了テストを返します。 |
int errorCount()
|
記録されたエラーの数を返します。 |
int failureCount()
|
記録された失敗の数を返します。 |
int notImplementedCount()
|
未完了のテストケースの数を返します。 |
int count()
|
実行したテストケースの総数を返します。 |
boolean wasSuccessful()
|
すべてのテストの実行に成功したかどうかを返します。 |
boolean allCompletlyImplemented()
|
すべてのテストが完全に実装されているかどうかを返します。 |
void collectCodeCoverageInformation(bool $flag)
|
コードカバレッジ情報の収集を有効あるいは無効にします。 |
array getCodeCoverageInformation()
|
収集したコードカバレッジ情報を返します。 |
PHPUnit_Framework_TestResult のオブザーバを登録したい場合は、PHPUnit_Framework_TestListener を実装する必要があります。これを登録するには、 表 21.13 に示した addListener() を使用します。
表21.13 TestResult および TestListener
| メソッド | 意味 |
|---|---|
void addListener(PHPUnit_Framework_TestListener $listener)
|
$listener を登録し、テスト結果の内容が更新された場合にその内容を受け取るようにします。
|
void removeListener(PHPUnit_Framework_TestListener $listener)
|
更新を受け取る $listener の登録を解除します。
|
表 21.14 に、テストリスナーが実装するメソッドを示します。 例 22.3 も参照ください。
表21.14 TestListener のコールバック
| メソッド | 意味 |
|---|---|
void addError(PHPUnit_Framework_Test $test, Exception $e)
|
$test が $e をスローしました。
|
void addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e)
|
$test がアサーションに失敗し、PHPUnit_Framework_AssertionFailedError 系がスローされました。
|
void addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e)
|
$test は完了しませんでした。
|
void addSkippedTest(PHPUnit_Framework_Test $test, Exception $e)
|
$test は実行されませんでした。
|
void startTestSuite(PHPUnit_Framework_TestSuite $suite)
|
$suite の実行が始まります。
|
void endTestSuite(PHPUnit_Framework_TestSuite $suite)
|
$suite の実行が終了しました。
|
void startTest(PHPUnit_Framework_Test $test)
|
$test の実行が始まります。
|
void endTest(PHPUnit_Framework_Test $test)
|
$test の実行が終了しました。
|
| Prev | Next |
Copyright © 2005-2011 Sebastian Bergmann.