PHPUnitのアサーションメソッドを知ろう! - RAKUS Developers Blog
PHPプログラムとして作成可能なユニットテスト
テスト駆動開発
TDD Boot Camp 2020 Online #1 基調講演/ライブコーディング
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);
}
}
// 以下のアサーションは全て成功になる
$this->assertEquals(1, '1');
$this->assertEquals(null, '');
$this->assertEquals(null, false);
$this->assertEquals(null, 0);
$this->assertEquals('1', true);
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
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.
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.
// 以下2つは同じ内容のテスト
$this->assertStringStartsWith('foo', $string);
$this->assertSame(0, strpos($string, 'foo')); // 何をテストしたいのか理解しにくい
// 以下2つは同じ内容のテスト
$this->assertStringEndsWith('bar', $string);
$this->assertSame('bar', substr($string, -3)); // これも理解しにくい
assertStringContainsString
よりも複雑な上限のテストが作成可能値が配列かどうかの検査方法
その他にも以下が存在
オブジェクトの型検査
or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing