You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A scenario always starts with actor class initialization. After that, writing a scenario is just like typing `$I->`
95
-
and choosing a proper action from the auto-completion list. Let's log in to our website:
91
+
This will generate `SigninCest.php` file inside `tests/acceptance` directory. Let's open it:
96
92
97
93
{% highlight php %}
98
94
99
95
<?php
100
-
$I = new AcceptanceTester($scenario); // actor class initialization
101
-
$I->wantTo('login to website');
102
-
103
-
104
-
{% endhighlight %}
105
-
106
-
The `wantTo` section describes your scenario in brief. There are additional comment methods that are useful to describe the context of a scenario:
107
-
108
-
{% highlight php %}
96
+
class SigninCest
97
+
{
98
+
function _before(AcceptanceTester $I)
99
+
{
100
+
}
101
+
102
+
public function _after(AcceptanceTester $I)
103
+
{
104
+
}
109
105
110
-
<?php
111
-
$I = new AcceptanceTester($scenario);
112
-
$I->am('user'); // actor's role
113
-
$I->wantTo('login to website'); // feature to test
114
-
$I->lookForwardTo('access website features for logged-in users'); // result to achieve
106
+
public function tryToTest(AcceptanceTester $I)
107
+
{
108
+
// todo: write test
109
+
}
110
+
}
115
111
116
112
{% endhighlight %}
117
113
118
-
After we have described the story background, let's start writing a scenario.
114
+
We have `_before` and `_after` methods to run some common actions before and after a test. And we have a placeholder action `tryToTest` which we need to implement.
115
+
If we try to test a signin process it's a good start to test a successful signin. Let's rename this method to `signInSuccessfully`.
119
116
120
-
We'll assume that we have a 'login' page where we get authenticated by providing a username and password.
117
+
We'll assume that we have a 'login' page where we get authenticated by providing a username and password.
121
118
Then we are sent to a user page, where we see the text `Hello, %username%`. Let's look at how this scenario is written in Codeception:
122
119
123
120
{% highlight php %}
124
121
125
122
<?php
126
-
$I = new AcceptanceTester($scenario);
127
-
$I->am('user');
128
-
$I->wantTo('login to website');
129
-
$I->lookForwardTo('access website features for logged-in users');
130
-
$I->amOnPage('/login');
131
-
$I->fillField('Username','davert');
132
-
$I->fillField('Password','qwerty');
133
-
$I->click('Login');
134
-
$I->see('Hello, davert');
123
+
class SigninCest
124
+
{
125
+
public function loginSuccessfully(AcceptanceTester $I)
126
+
{
127
+
$I->amOnPage('/login');
128
+
$I->fillField('Username','davert');
129
+
$I->fillField('Password','qwerty');
130
+
$I->click('Login');
131
+
$I->see('Hello, davert');
132
+
}
133
+
}
135
134
136
135
{% endhighlight %}
137
136
138
137
This scenario can probably be read by non-technical people. If you just remove all special chars like braces, arrows and `$`,
139
138
this test transforms into plain English text:
140
139
141
140
{% highlight yaml %}
142
-
I am user
143
-
I wantTo login to website
144
-
I lookForwardTo access website features for logged-in users
145
141
I amOnPage '/login'
146
142
I fillField 'Username','davert'
147
143
I fillField 'Password','qwerty'
@@ -154,7 +150,7 @@ Codeception generates this text representation from PHP code by executing:
154
150
155
151
{% highlight bash %}
156
152
157
-
php codecept generate:scenarios
153
+
php vendor/bin/codecept generate:scenarios
158
154
159
155
{% endhighlight %}
160
156
@@ -178,7 +174,7 @@ After configuring the URL we can run this test with the `run` command:
178
174
179
175
{% highlight bash %}
180
176
181
-
php codecept run
177
+
php vendor/bin/codecept run
182
178
183
179
{% endhighlight %}
184
180
@@ -187,7 +183,7 @@ This is the output we should see:
I look forward to access website features for logged-in users
218
212
I am on page "/login"
219
213
I fill field "Username" "davert"
220
214
I fill field "Password" "qwerty"
@@ -232,58 +226,45 @@ OK (1 test, 1 assertions)
232
226
This simple test can be extended to a complete scenario of site usage, therefore,
233
227
by emulating the user's actions, you can test any of your websites.
234
228
235
-
Give it a try!
236
-
237
-
## Cept, Cest and Test Formats
229
+
To run more tests create a public method for each of them. Include `AcceptanceTester` object as `$I` as a method parameter and use the same `$I->` API you've seen before.
230
+
If your tests share common setup actions put them into `_before` method.
238
231
239
-
Codeception supports three test formats. Beside the previously described scenario-based Cept format,
240
-
Codeception can also execute [PHPUnit test files for unit testing](http://codeception.com/docs/05-UnitTests), and Cest format.
241
-
242
-
**Cest** combines scenario-driven test approach with OOP design. In case you want to group a few testing scenarios into one, you should consider using Cest format.
243
-
In the example below we are testing CRUD actions within a single file but with several tests (one per operation):
232
+
For instance, to test CRUD we want 4 methods to be implemented and all next tests should start at `/task` page:
244
233
245
234
{% highlight php %}
246
235
247
236
<?php
248
-
class PageCrudCest
237
+
class TaskCrudCest
249
238
{
250
239
function _before(AcceptanceTester $I)
251
240
{
252
241
// will be executed at the beginning of each test
253
-
$I->amOnPage('/');
242
+
$I->amOnPage('/task');
254
243
}
255
244
256
-
function createPage(AcceptanceTester $I)
245
+
function createTask(AcceptanceTester $I)
257
246
{
258
247
// todo: write test
259
248
}
260
249
261
-
function viewPage(AcceptanceTester $I)
250
+
function viewTask(AcceptanceTester $I)
262
251
{
263
252
// todo: write test
264
253
}
265
254
266
-
function updatePage(AcceptanceTester $I)
255
+
function updateTask(AcceptanceTester $I)
267
256
{
268
257
// todo: write test
269
258
}
270
259
271
-
function deletePage(AcceptanceTester $I)
260
+
function deleteTask(AcceptanceTester $I)
272
261
{
273
262
// todo: write test
274
263
}
275
264
}
276
265
277
266
{% endhighlight %}
278
267
279
-
Cest files such as this can be created by running a generator:
280
-
281
-
{% highlight bash %}
282
-
283
-
php codecept generate:cest acceptance PageCrud
284
-
285
-
{% endhighlight %}
286
-
287
268
Learn more about the [Cest format](http://codeception.com/docs/07-AdvancedUsage#Cest-Classes) in the Advanced Testing section.
288
269
289
270
## BDD
@@ -303,47 +284,47 @@ Tests can be started with the `run` command:
303
284
304
285
{% highlight bash %}
305
286
306
-
php codecept run
287
+
php vendor/bin/codecept run
307
288
308
289
{% endhighlight %}
309
290
310
291
With the first argument you can run all tests from one suite:
311
292
312
293
{% highlight bash %}
313
294
314
-
php codecept run acceptance
295
+
php vendor/bin/codecept run acceptance
315
296
316
297
{% endhighlight %}
317
298
318
299
To limit tests run to a single class, add a second argument. Provide a local path to the test class, from the suite directory:
319
300
320
301
{% highlight bash %}
321
302
322
-
php codecept run acceptance SigninCept.php
303
+
php vendor/bin/codecept run acceptance SigninCest.php
323
304
324
305
{% endhighlight %}
325
306
326
307
Alternatively you can provide the full path to test file:
327
308
328
309
{% highlight bash %}
329
310
330
-
php codecept run tests/acceptance/SigninCept.php
311
+
php vendor/bin/codecept run tests/acceptance/SigninCest.php
331
312
332
313
{% endhighlight %}
333
314
334
315
You can further filter which tests are run by appending a method name to the class, separated by a colon (for Cest or Test formats):
335
316
336
317
{% highlight bash %}
337
318
338
-
php codecept run tests/acceptance/SignInCest.php:^anonymousLogin$
319
+
php vendor/bin/codecept run tests/acceptance/SigninCest.php:^anonymousLogin$
339
320
340
321
{% endhighlight %}
341
322
342
323
You can provide a directory path as well. This will execute all acceptance tests from the `backend` dir:
343
324
344
325
{% highlight bash %}
345
326
346
-
php codecept run tests/acceptance/backend
327
+
php vendor/bin/codecept run tests/acceptance/backend
347
328
348
329
{% endhighlight %}
349
330
@@ -352,7 +333,7 @@ For example, this will execute all acceptance tests from the `backend` dir begin
352
333
353
334
{% highlight bash %}
354
335
355
-
php codecept run tests/acceptance/backend:^login
336
+
php vendor/bin/codecept run tests/acceptance/backend:^login
356
337
357
338
{% endhighlight %}
358
339
@@ -364,7 +345,7 @@ To generate JUnit XML output, you can provide the `--xml` option, and `--html` f
364
345
365
346
{% highlight bash %}
366
347
367
-
php codecept run --steps --xml --html
348
+
php vendor/bin/codecept run --steps --xml --html
368
349
369
350
{% endhighlight %}
370
351
@@ -374,7 +355,7 @@ To see all the available options, run the following command:
374
355
375
356
{% highlight bash %}
376
357
377
-
php codecept help run
358
+
php vendor/bin/codecept help run
378
359
379
360
{% endhighlight %}
380
361
@@ -387,7 +368,6 @@ You may print any information inside a test using the `codecept_debug` function.
387
368
388
369
There are plenty of useful Codeception commands:
389
370
390
-
* `generate:cept` *suite* *filename* - Generates a sample Cept scenario
391
371
* `generate:cest` *suite* *filename* - Generates a sample Cest test
392
372
* `generate:test` *suite* *filename* - Generates a sample PHPUnit Test with Codeception hooks
0 commit comments