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
Copy file name to clipboardExpand all lines: guides/07-AdvancedUsage.md
+28-28Lines changed: 28 additions & 28 deletions
Original file line number
Diff line number
Diff line change
@@ -1,13 +1,13 @@
1
1
# Advanced Usage
2
2
3
-
In this chapter we will cover some techniques and options that you can use to improve your testing experience
3
+
In this chapter, we will cover some techniques and options that you can use to improve your testing experience
4
4
and keep your project better organized.
5
5
6
6
## Cest Classes
7
7
8
-
Cest is a common test format for Codeception, it is "Test" firth first C letter in it.
9
-
It is scenario-driven format so all tests written in it are executed step by steps.
10
-
Unless you need direct access to application code inside a test, Cest format is recommended to use.
8
+
Cest is a common test format for Codeception, it is "Test" with the first C letter in it.
9
+
It is scenario-driven format so all tests written in it are executed step by step.
10
+
Unless you need direct access to application code inside a test, Cest format is recommended.
11
11
As it provides more readable code for functional, api, and acceptance tests.
12
12
13
13
A new Cest class can be created via `g:cest` command:
@@ -75,8 +75,8 @@ that may be used in child classes. But don't forget to make these methods `prote
75
75
76
76
Cest format also can contain hooks based on test results:
77
77
78
-
*`_failed` will be executed on failed test
79
-
*`_passed` will be executed on passed test
78
+
*`_failed` will be executed for failed test
79
+
*`_passed` will be executed for passed test
80
80
81
81
```php
82
82
public function _failed(AcceptanceTester $I)
@@ -117,7 +117,7 @@ class UserCest {
117
117
}
118
118
```
119
119
120
-
If you need skipping a test on condition, inject `\Codeception\Scenario` into the test:
120
+
If you need to skip a test on a condition, inject `\Codeception\Scenario` into the test:
121
121
122
122
```php
123
123
public function worksOnCondition(AcceptanceTester $I, \Codeception\Scenario $scenario)
@@ -133,7 +133,7 @@ public function worksOnCondition(AcceptanceTester $I, \Codeception\Scenario $sce
133
133
}
134
134
```
135
135
136
-
For unit tests tests can be skipped via attribute or by `markTestSkipped` method:
136
+
Unit tests can be skipped via the attribute or by using`markTestSkipped` method:
137
137
138
138
```php
139
139
<?php
@@ -166,7 +166,7 @@ class UserTest extends \Codeception\Test\Unit
166
166
167
167
## Incomplete Tests
168
168
169
-
Tests can be marked as Incomplete, in this case they also will be skipped.
169
+
Tests can be marked as Incomplete, in this case, they also will be skipped.
170
170
To mark a test as incomplete use `Codeception\Attribute\Incomplete` which should be used similarly to `Skip` attribute:
171
171
172
172
```php
@@ -199,7 +199,7 @@ You can execute one (or several) specific groups of tests:
199
199
php vendor/bin/codecept run -g admin -g editor
200
200
```
201
201
202
-
The concept of groups was taken from PHPUnit and behave in the same way.
202
+
The concept of groups was taken from PHPUnit and behaves in the same way.
203
203
204
204
For Test and Cest files you can use the `Group` attribute to add a test to a group.
205
205
@@ -213,7 +213,7 @@ use Tests\Support\AcceptanceTester
213
213
class UserCest {
214
214
215
215
#[Group('admin')] // set a group for this test
216
-
#[Group('slow', 'important')] // add more groups
216
+
#[Group('slow', 'important')] // add groups in a single attribute
217
217
public function testAdminUser(AcceptanceTester $I)
218
218
{
219
219
}
@@ -345,7 +345,7 @@ class ModeratorCest {
345
345
346
346
### Dependencies
347
347
348
-
With the `Depends` attribute you can specify a test that should be passed before the current one.
348
+
With the `Depends` attribute, you can specify a test that should be passed before the current one.
349
349
If that test fails, the current test will be skipped. You should pass the method name of the test you are relying on.
350
350
351
351
```php
@@ -385,11 +385,11 @@ Codeception reorders tests so dependent tests will always be executed before the
385
385
386
386
## Environments
387
387
388
-
For cases where you need to run tests with different configurations you can define different config environments.
388
+
For cases where you need to run tests with different configurations, you can define different config environments.
389
389
The most typical use cases are running acceptance tests in different browsers,
390
390
or running database tests using different database engines.
391
391
392
-
Let's demonstrate the usage of environments for the browsers case.
392
+
Let's demonstrate the usage of environments for multi-browser testing.
393
393
394
394
We need to add some new lines to `acceptance.suite.yml`:
395
395
@@ -427,8 +427,7 @@ paths:
427
427
428
428
```
429
429
430
-
The names of these files are used as environments names
431
-
(e.g. `chrome.yml` or `chrome.dist.yml` for an environment named `chrome`).
430
+
The names of these files are used as environment names (e.g. `chrome.yml` or `chrome.dist.yml` for an environment named `chrome`).
432
431
You can generate a new file with this environment configuration by using the `generate:environment` command:
433
432
434
433
```
@@ -496,6 +495,12 @@ class UserCest
496
495
}
497
496
```
498
497
498
+
Multiple values can be set in one attribute:
499
+
500
+
```php
501
+
#[Env('chrome', 'firefox')]
502
+
```
503
+
499
504
This way you can easily control which tests will be executed for each environment.
500
505
501
506
It is possible to combine environments. For instance, if a test should be executed only in chrome on staging, this can be declared as a combined environment:
@@ -512,9 +517,9 @@ php vendor/bin/codecept run --env chrome,staging
512
517
513
518
## Get Test Metadata
514
519
515
-
Sometimes you may need to change the test behavior in realtime.
520
+
Sometimes you may need to change the test behavior in real-time.
516
521
For instance, the behavior of the same test may differ in Firefox and in Chrome.
517
-
In runtime we can retrieve the current environment name, test name,
522
+
In runtime, we can retrieve the current environment name, test name,
518
523
or list of enabled modules by calling the `$scenario->current()` method.
519
524
520
525
```php
@@ -554,7 +559,7 @@ public function myTest(\Codeception\Scenario $scenario)
554
559
555
560
## Shuffle
556
561
557
-
By default Codeception runs tests in alphabetic order.
562
+
By default, Codeception runs tests in alphabetic order.
558
563
To ensure that tests are not depending on each other (unless explicitly declared via `@depends`) you can enable `shuffle` option.
559
564
560
565
```yaml
@@ -564,15 +569,15 @@ settings:
564
569
565
570
```
566
571
567
-
Alternatively, you may run tests in shuffle without changing the config:
572
+
Alternatively, you may run tests in the shuffle without changing the config:
568
573
569
574
```yaml
570
575
codecept run -o "settings: shuffle: true"
571
576
```
572
577
573
578
574
-
Tests will be randomly reordered on each run. When tests executed in shuffle mode a seed value will be printed.
575
-
Copy this seed value from output to be able to rerun tests in the same order.
579
+
Tests will be randomly reordered on each run. When tests are executed in shuffle mode a seed value will be printed.
580
+
Copy this seed value from the output to be able to rerun tests in the same order.
576
581
577
582
```yaml
578
583
$ codecept run
@@ -663,7 +668,7 @@ which can be constructed with arguments known to Codeception.
663
668
664
669
In order to make auto-wiring work, you will need to implement the `_inject()` method with the list of desired arguments.
665
670
It is important to specify the type of arguments, so Codeception can guess which objects are expected to be received.
666
-
The `_inject()` will only be invoked once, just after creation of the TestCase object (either Cest or Test).
671
+
The `_inject()` will only be invoked once, just after the creation of the TestCase object (either Cest or Test).
667
672
Dependency Injection will also work in a similar manner for Helper and Actor classes.
668
673
669
674
Each test of a Cest class can declare its own dependencies and receive them from method arguments:
@@ -694,9 +699,4 @@ Moreover, Codeception can resolve dependencies recursively (when `A` depends on
694
699
and handle parameters of primitive types with default values (like `$param = 'default'`).
695
700
Of course, you are not allowed to have *cyclic dependencies*.
696
701
697
-
## Conclusion
698
-
699
-
Codeception is a framework which may look simple at first glance
700
-
but it allows you to build powerful tests with a single API, refactor them,
701
-
and write them faster using the interactive console. Codeception tests can be easily organized in groups or Cest classes.
0 commit comments