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: docs/06-UnitTests-TEST.markdown
+25-54Lines changed: 25 additions & 54 deletions
Original file line number
Diff line number
Diff line change
@@ -38,24 +38,25 @@ class SimpleTest extends \Codeception\TestCase\Test
38
38
*/
39
39
protected $codeGuy;
40
40
41
-
// keep this setupUp and tearDown to enable proper work of Codeception modules
42
-
protected function setUp()
41
+
// executed before each test
42
+
protected function _before()
43
43
{
44
-
if ($this->bootstrap) require $this->bootstrap;
45
-
$this->dispatcher->dispatch('test.before', new \Codeception\Event\Test($this));
46
-
$this->codeGuy = new CodeGuy($scenario = new \Codeception\Scenario($this));
47
-
$scenario->run();
48
44
}
49
45
50
-
protected function tearDown()
46
+
// executed after each test
47
+
protected function _after()
51
48
{
52
-
$this->dispatcher->dispatch('test.after', new \Codeception\Event\Test($this));
53
49
}
54
50
}
55
51
?>
56
52
57
53
{% endhighlight %}
58
-
This class has predefined `setUp` and `tearDown` methods to start with. They are used to include a bootstrap file (`_bootstrap.php` by default) and set up the codeGuy class to have all the cool actions from Cept-files to be run as a part of unit tests. Just like in accordance tests, you can choose the proper modules for `CodeGuy` class in `unit.suite.yml` configuration file.
54
+
This class has predefined `_before` and `_after` methods to start with. You can use them to create a tested object before each test, and destroy it afterwards.
55
+
56
+
As you see unlike in PHPUnit setUp/tearDown methods are replaced with their aliases: `_before`, `_after`.
57
+
The actual setUp and tearDown was implemented by parent class `\Codeception\TestCase\Test` and is used to include a bootstrap file (`_bootstrap.php` by default) and set up the codeGuy class to have all the cool actions from Cept-files to be run as a part of unit tests. Just like in accordance tests, you can choose the proper modules for `CodeGuy` class in `unit.suite.yml` configuration file.
58
+
So If you implement `setUp` and `tearDown` be sure, that you will call their parent method.
59
+
59
60
60
61
{% highlight yaml %}
61
62
yaml
@@ -107,52 +108,32 @@ function testSavingUser()
107
108
108
109
{% endhighlight %}
109
110
110
-
The setUp and tearDown methods of your test will perform all initialization and cleanup actions for modules included into CodeGuy class. For instance, while using the database module will make it clean up the database after each run. If you don't need this cleanup to performed you can either update the suite configuration file or just remove this lines for your test:
111
-
112
-
{% highlight php %}
113
-
114
-
<?php
115
-
// in setUp
116
-
$this->dispatcher->dispatch('test.before', new \Codeception\Event\Test($this));
117
-
// in tearDown
118
-
$this->dispatcher->dispatch('test.after', new \Codeception\Event\Test($this));
119
-
?>
111
+
Database will be cleaned and populated after each test, as it happens for acceptance and functional tests.
112
+
If it's not your required behavior, please change the settings of `Db` module for current suite.
120
113
121
-
{% endhighlight %}
114
+
### Modules
122
115
123
-
### Bootstrap
116
+
*new in 1.5.2*
124
117
125
-
The bootstrap file is located in suite directory and is named `_bootstrap`. It's widely used in acceptance and functional tests to initialze the predefined variables. In unit tests it can be used for sharing share same data among the different tests. But the main purpose of is to set up an autoloader for your project inside this class. Otherwise Codeception will not find the testing classes and fail.
118
+
Codeception allows you to access properties and methods of all modules defined for this suite. Unlike using the CodeGuy class for this purpose, using module directly grants you access to all public properties of that module.
126
119
127
-
Example bootstrap file may look like this:
120
+
For example, if you use `Symfony2` module here is the way you can access Symfony container:
The `$demoUser` varible now can be accessed in setUp method of a test class.
132
+
All public variables are listed in references for corresponding modules.
141
133
142
-
{% highlight php %}
143
-
144
-
<?php
145
-
protected function setUp()
146
-
{
147
-
if ($this->bootstrap) require $this->bootstrap;
148
-
$this->user = $demoUser;
149
-
// ...
150
-
}
151
-
?>
152
-
153
-
{% endhighlight %}
134
+
### Bootstrap
154
135
155
-
And you can use it anywhere in your test. Use autoloader to set up the fixtures for your tests. For more information on working with data in tests - read the _Data_ chapter.
136
+
The bootstrap file is located in suite directory and is named `_bootstrap` and is **included before each test** (with `setUp` method in parent class). It's widely used in acceptance and functional tests to initialize the predefined variables. In unit tests it can be used for sharing share same data among the different tests. But the main purpose of is to set up an autoloader for your project inside this class. Otherwise Codeception will not find the testing classes and fail.
156
137
157
138
### Stubs
158
139
@@ -176,19 +157,9 @@ class SimpleTest extends \Codeception\TestCase\Test
176
157
*/
177
158
protected $codeGuy;
178
159
179
-
// keep this setupUp and tearDown to enable proper work of Codeception modules
180
-
protected function setUp()
181
-
{
182
-
if ($this->bootstrap) require $this->bootstrap;
183
-
$this->user = new DemoUser;
184
-
$this->dispatcher->dispatch('test.before', new \Codeception\Event\Test($this));
185
-
$this->codeGuy = new CodeGuy($scenario = new \Codeception\Scenario($this));
186
-
$scenario->run();
187
-
}
188
-
189
-
protected function tearDown()
160
+
function _before()
190
161
{
191
-
$this->dispatcher->dispatch('test.after', new \Codeception\Event\Test($this));
Copy file name to clipboardExpand all lines: docs/08-UnitTests-CEST.markdown
+29-3Lines changed: 29 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -30,7 +30,35 @@ class UserCest {
30
30
31
31
{% endhighlight %}
32
32
33
-
In most cases, we will probably test exactly one method. As we discovered, it's quite easy to define the class and method you are going to test. We take the `$class` parameter of the Cest class, and the method's name as a target method.
33
+
Let's create first test with `generate:cest` command:
34
+
35
+
{% highlight yaml %}
36
+
bash
37
+
$ php codecept.phar generate:cest unit Post
38
+
39
+
{% endhighlight %}
40
+
41
+
At first we need to define with `$class` preoperty the class which is being actually tested.
42
+
43
+
{% highlight php %}
44
+
45
+
<?php
46
+
class PostCest {
47
+
$class = 'Post';
48
+
49
+
function shouldBe(CodeGuy $I)
50
+
{
51
+
52
+
}
53
+
}
54
+
?>
55
+
56
+
{% endhighlight %}
57
+
58
+
59
+
This will create an empty Cest file for us.
60
+
61
+
There are many cases whre we test only one method of a class. As we discovered, it's quite easy to define the class and method you are going to test. We take the `$class` parameter of the Cest class, and the method's name as a target method.
34
62
35
63
{% highlight php %}
36
64
@@ -119,8 +147,6 @@ Example bootstrap file (`tests/unit/_bootstrap.php`)
0 commit comments