主要透过setUp()与tearDown()分别来初始化测试与还原到初始化前的状态。
代码如下:
public function setUp() {
$eventId = 1;
$eventName = '活动1';
$eventStartDate = '2016-11-01 18:00:00';
$eventEndDate = '2016-11-01 20:00:00';
$eventAttendLimit = 10;
$this->event = new \PHPUnitEventDemo\Event($eventId, $eventName, $eventStartDate, $eventEndDate, $eventAttendLimit);
$userId = 1;
$userName = 'User1';
$userEmail = 'user1@zoco.space';
$this->user = new \PHPUnitEventDemo\User($userId, $userName, $userEmail);
}
public function tearDown() {
$this->event = null;
$this->user = null;
}
把$event,$user类修改成全局变量,接着把构造类写在setUp()中,析构类写在tearDown(),testReserve()与testDuplicatedReservationWithException中使用这两个变量。
所以在执行测试的时候,运行顺序会是:
原文转自:http://www.jianshu.com/p/ba6829a6f3ec