More Related Content Similar to 第一次用 PHPUnit 寫測試就上手 Similar to 第一次用 PHPUnit 寫測試就上手 (20) 第一次用 PHPUnit 寫測試就上手2. ⼤大綱
‣ 介紹 unit testing
‣ 簡介 PHPUnit
‣ PHPUnit 提供的功能
- Assertion, Test Dependencies,
- Data Providers, Test Exception, Fixtures
‣ 組織及設定 PHPUnit
‣ Code Coverage
8. 1. 建⽴立 20 個以上的使⽤用者
2. 分別以這些使⽤用者的⾝身份報名活動
崩╰(〒⽫皿〒)╯潰
這不是測試!
13. 對程式中最⼩小單位 (unit) 進⾏行測試
如 function 或 class 中的 method
function reserve($user)
{
// 使⽤用者報名此活動
}
public function testReserve()
{
// 使⽤用者報名此活動
$this->assertCount($expectedCount,
$this->event->attendees);
}
⺫⽬目標程式 測試程式
16. public function reserve($user)
{
// 使⽤用者報名
if (array_key_exists($user->id, $this->attendees)) {
throw new PHPUnitEventDemoEventException(
'Duplicated reservation',
PHPUnitEventDemoEventException::DUPLICATED_RESERVATION
);
}
if ($this->attendee_limit > $this->getAttendeeNumber()) {
$this->attendees[$user->id] = $user;
return true;
}
return false;
}
測試即⽂文件
⺫⽬目標程式
17. 測試即⽂文件
public function testReserve()
{
// 測試報名活動
}
/**
* @dataProvider eventsProvider
*/
public function testAttendeeLimitReserve(...)
{
// 測試限制報名⼈人數
}
/**
* @expectedException PHPUnitEventDemoEventException
* @expectedExceptionMessage Duplicated reservation
* @expectedExceptionCode 1
*/
public function testDuplicatedReservationWithException()
{
// 測試重複報名異常
}
19. 1. 容易撰寫及維護
public function testReserveAndUnreserve()
{
// 測試參加活動
$userId = 1;
$userName = 'A User';
$user = new PHPUnitEventDemoUser($userId, $userName);
$eventId = 1;
$eventName = 'Demo Event';
$eventStartDate = '2014-11-30 13:30:00';
$eventEndDate = '2014-11-30 16:30:00';
$eventDeadline = '2014-11-29 23:59:59';
$eventAttendeeLimit = 15;
$event = new PHPUnitEventDemoEvent(
$eventId, $eventName, $eventStartDate,
$eventEndDate, $eventDeadline,
$eventAttendeeLimit
);
$event->reserve($user);
$expectedCount = 1;
$this->assertCount($expectedCount, $event->attendees);
$this->assertContains($user, $event->attendees);
$event->unreserve($user);
$this->assertCount(0, $event->attendees);
$this->assertNotContains($user, $event->attendees);
}
建⽴立物件
預期判斷
✗
20. public function testReserveAndUnreserve()
{
// 測試參加活動
$userId = 1;
$userName = 'A User';
$user = new PHPUnitEventDemoUser($userId, $userName);
$eventId = 1;
$eventName = 'Demo Event';
$eventStartDate = '2014-11-30 13:30:00';
$eventEndDate = '2014-11-30 16:30:00';
$eventDeadline = '2014-11-29 23:59:59';
$eventAttendeeLimit = 15;
$event = new PHPUnitEventDemoEvent(
$eventId, $eventName, $eventStartDate,
$eventEndDate, $eventDeadline,
$eventAttendeeLimit
);
$event->reserve($user);
$expectedCount = 1;
$this->assertCount($expectedCount, $event->attendees);
$this->assertContains($user, $event->attendees);
$event->unreserve($user);
$this->assertCount(0, $event->attendees);
$this->assertNotContains($user, $event->attendees);
}
測試多個功能
2. ⼀一個測試案例只測試單⼀一功能
✗
46. $ phpunit --bootstrap vendor/autoload.php tests/EventTest
PHPUnit 4.3.5 by Sebastian Bergmann.
...PHP Fatal error: Call to a member function unreserve() on a non-object
in /Users/aming/git/PHPUnit-Event-Demo/tests/EventTest.php on line 114
...
Fatal error: Call to a member function unreserve() on a non-object in /
Users/aming/git/PHPUnit-Event-Demo/tests/EventTest.php on line 114
...
造成 testUnreserve(),無法取得傳⼊入的物件
Depends 與 Data Provider
50. Test Exceptions
‣ 預期測試⽅方法內拋出異常
‣ 利⽤用 3 個標註:
- @expectedException ExceptionClass
- @expectedExceptionMessage ExceptionMessage
- @expectedExceptionCode ExceptionCode
62. 組織及設定 PHPUnit
$ phpunit --configuration phpunit.xml --testsuite MyEventTests
PHPUnit 4.3.5 by Sebastian Bergmann.
Configuration read from /Users/aming/git/PHPUnit-Event-Demo/phpunit.xml
......
Time: 5.82 seconds, Memory: 12.75Mb
OK (6 tests, 20 assertions)
Generating code coverage report in HTML format ... done
--configuration <config_file>phpunit 讀取設定檔
65. Code Coverage
$ phpunit --bootstrap vendor/autoload.php --coverage-html report/ tests/
PHPUnit 4.3.5 by Sebastian Bergmann.
.........
Time: 3.49 seconds, Memory: 13.25Mb
OK (9 tests, 17 assertions)
Generating code coverage report in HTML format ... done
phpunit tool 中設定 --coverage-html <dir>
67. More information
‣ PHPUnit official site - https://phpunit.de/
‣ 《PHPUnit Essentials》- ISBN 978-1-78328-343-9
‣ xUnit Patterns - http://xunitpatterns.com/
‣ Demo 程式碼 - http://goo.gl/cEYfv4
‣ 練習專案 - http://goo.gl/9bFnWJ
‣ 練習環境架設 - http://goo.gl/ppBlz1