PHP Test Case 的 Codeception 筆記


之前提到在用 PHPUnit 的 Selenium.
不過由於每次啟動都會開瀏覽器作測試.會影響正常工作.
換上自我感覺更強大的 Codeception 就方便很多.
全都在 Console 進行.也可以整合 Selenium.
這只記錄 Console 部份.記錄一下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# 加入 Codeception
"require-dev": {
"Codeception/Codeception": "1.5.5"
},

# 取得 Codeception
php composer.phar update

# 初始化 Codeception
php vendor/bin/codecept bootstrap

# =====================
# 建立第一個測試程式 (acceptance)
php vendor/bin/codecept generate:cept acceptance signup/SubmitExistsAccount

# 修改網站目錄 tests/acceptance.suite.xml

config:
PhpBrowser:
url: 'http://localhost/work/project/'

# 更新一下已產生的程式 (如果有改動 module)
php vendor/bin/codecept build

# 修改 tests/acceptance/signup/SubmitExistsAccountCept.php 內容如下
<?php
$I = new WebGuy($scenario);
$I->wantTo('submit exists account');
$I->amOnPage('/signup');
$I->submitForm('form', array(
'email' => '[email protected]',
'password' => 'testtest',
'confirm_password' => 'testtest',
));
$I->see('Error! The email address already exists', '.alert-error');

# 執行全部測試程式
php vendor/bin/codecept run

# 執行 appceptance 裡的 signup 目錄中的測試程式
php vendor/bin/codecept run acceptance signup

# 比較友好的測試訊息
php vendor/bin/codecept run -steps

# =====================
# 建立第一個測試程式 (unit)
# 在 tests/unit.suite.yml 加入 PhpBrowser 模組

class_name: CodeGuy
modules:
enabled: [Unit, CodeHelper, PhpBrowser]
config:
PhpBrowser:
url: 'http://localhost/work/pet/'

# 重建一下檔案,使 CodeGuy 加入 PhpBrowser 模組
php vendor/bin/codecept build

# 建立測試的檔案
php vendor/bin/codecept generate:test unit Signup

# 同一個測試內容.相關的內容大約如下
<?php
use Codeception\Util\Stub;

class SignupTest extends \Codeception\TestCase\Test
{
/**
* @var \CodeGuy
*/
protected $codeGuy;

protected function _before()
{
}

protected function _after()
{
}

// tests
public function testSubmitExistsAccount()
{
$this->codeGuy->amOnPage('/signup');
$this->codeGuy->submitForm('form', array(
'email' => '[email protected]',
'password' => 'testtest',
'confirm_password' => 'testtest',
));
$this->codeGuy->see('Error! The email address already exists', '.alert-error');
}

}