PHPUnit5.7.0で配列操作のテストをしてみる。
参考にしたドキュメントのテストコード。
use PHPUnit\Framework\TestCase;
class StackTest extends Testcase
{
public function testPushAndPop()
{
$stack = [];
$this->assertEquals(0, count($stack));
array_push($stack, 'foo');
$this->assertEquals('foo', $stack[count($stack) - 1]);
$this->assertEquals(1, count($stack));
$this->assertEquals('foo', array_pop($stack));
$this->assertEquals(0, count($stack));
}
}
テストを実行する。結果、すべてOKになった。
> phpunit test.php
PHPUnit 5.7.0 by Sebastian Bergmann and contributors.
. 1 / 1 (100%)
Time: 225 ms, Memory: 2.25MB
OK (1 test, 5 assertions)
試しに、配列の数が1つであることを確認している箇所を2にしてから実行する。
すると、当然だが、エラーが出力された。
2を予想しているところ、1だったこと。
テストコードの13行目で発生したこと。
3つ目のAssertでエラーが起きたこと。
などがわかる。
There was 1 failure:
1) StackTest::testPushAndPop
Failed asserting that 1 matches expected 2.
test.php:13
FAILURES!
Tests: 1, Assertions: 3, Failures: 1.

コメント