setup/teardown
testのmethodが実行されるたびに前処理・後処理
setUPBeforeClass/tearDownAfterClass
test クラス全体に対しての前処理・後処理
PHPUnit でよく使う Assertion
| MethodA | Meann |
|---|---|
| assertEquals($exp, $act) | $expと$actが等しい(==) |
| assertSame($exp, $act) | $expと$actが等しい(===) |
| assertNotEquals($exp, $act) | $expと$actが等しくない |
| assertContains(mixed $needle, Iterator array $haystack) | 配列に $needleが含まれている |
| assertArrayHasKey(mixed $key, array $array) | 配列にキーが含まれている |
| assertTrue($cond) | $condがTRUE |
| assertFalse($cond) | $condがFALSE |
| assertNull($var) | $varがNULL |
| assertNotNull($var) | $varがNULLではない |
| assertInternalType($type, $act) | $actの型が$typeと一致する |
| assertInstanceOf($expected, $actual) | $actualが$expectedのインスタンスである |
PHPUnit でよく使う Annotation
@test
class HogeTest extends TestCase
{
/** @test */
public function hogeMethod()
{
...
}
}/** @test */ を書くことで testHogeTest と書かなくて良い
dataProvider
class HogeTest extends TestCase
{
/**
* @test
* @dataProvider provider
*/
public function hoge($dollar, $multiplication, $expect)
{
...
...
}
/** @test */
public function provider(): array
{
return [
// $dollar, $multiplication, $expect
[5, 2, 10],
[15, 3, 45],
];
}
}@dataProvider provider テストデータ指定
@testWit
class HogeTest extends TestCase
{
/**
* @test
* @testWith [5, 2, 10]
* [15, 3, 45]
*/
public function hoge($dollar, $multiplication, $expect)
{
$dollar = new Dollar($dollar);
$this->assertEquals(new Dollar($expect), $dollar->times($multiplication));
}
}@testWith で PHPoc 内に データを記述
@testdox
/**
* @test
* @testdox 説明文
* @testWith [5, 2, 10]
* [15, 3, 45]
*/@testdoxに説明文をかける
@group
/**
* @group hogehoge
*/
public function hoge() {
// ...
}@group hogehoge group を指定して 実行できる
# 指定のグループのみ実行
./vendor/bin/phpunit tests/ --group hogehoge
# 指定のグループ以外実行
./vendor/bin/phpunit tests/ --exclude-group hogehoge@depends
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);
}
}- testEmptyの実行
- testEmptyの実行結果を引数に、testPushを実行
- testPushの実行結果を引数に、testPopを実行
@expectedException InvalidArgumentException
/**
* @expectedException InvalidArgumentException
**/例外を受け取ってもTestOKとする
