Skip to content

Commit dbb3eb4

Browse files
Recommending /tests directory instead of bundle-specific Tests-directories
Reason: This will come with Symfony 4: http://fabien.potencier.org/symfony4-directory-structure.html Pleasant side-effect: Several problems are suddenly disappearing (like e.g. Codeception/Codeception#4244 (comment) and point 2 at Codeception/Codeception#4270 (comment) as well as some other issues I encountered but haven't mentioned yet).
1 parent c1473f8 commit dbb3eb4

1 file changed

Lines changed: 21 additions & 41 deletions

File tree

for/symfony.md

Lines changed: 21 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ composer require codeception/codeception --dev
2929

3030
## Setup
3131

32-
It is recommended to have unit and functional testing set up per each bundle and acceptance tests for set globally. Unit and Functional tests will check that each bundle work as expected while acceptance tests will check the UI and work of a system as a whole.
32+
From Symfony 4 onwards there will be a top-level `tests` directory instead of a separate `Tests` directory in each bundle.
33+
So to save you from reconfiguration in the future, it is recommended place unit, functional, and acceptance test files
34+
into `/tests`.
3335

3436
### Project Setup
3537

@@ -110,36 +112,26 @@ php bin/codecept gherkin:snippets
110112
Continue to <a href="http://codeception.com/docs/07-BDD">Behavior Driven Development Guide &raquo;</a>
111113
</div>
112114
113-
## Bundle Setup
115+
## Functional Testing
114116
115-
Each bundle should have its own Codeception setup. To have test configuration for `AppBundle` you should run:
117+
There is no need to use `WebTestCase` to write functional tests. Symfony functional tests are written in the same manner as acceptance tests but are executed inside a framework. Codeception has the [Symfony Module](http://codeception.com/docs/modules/Symfony) for it. To create a functional test suite, run:
116118
117119
```
118-
php bin/codecept bootstrap --empty src/AppBundle --namespace AppBundle
120+
php bin/codecept g:suite functional
119121
```
120122
121-
This will create `tests` dir inside `src/AppBundle`.
122-
123-
### Functional Testing
124-
125-
There is no need to use `WebTestCase` to write functional tests. Symfony functional tests are written in the same manner as acceptance tests but are executed inside a framework. Codeception has `Symfony` module for it. It is a good idea to separate functional and unit tests so it is recommended to create a new test suite for AppBundle
126-
127-
```
128-
php bin/codecept g:suite functional -c src/AppBundle
129-
```
130-
131-
Functional tests are written in the same manner as acceptance tests. They also use scenario and `$I` actor object. The only difference is how they are executed. To run tests as Symfony test you should enable corresponding module in functional suite configureation file `src/Appundle/tests/functional.suite.yml`. Probably you want Doctrine to be included as well. Then you should use this configuration:
123+
Functional tests are written in the same manner as acceptance tests. They also use scenario and `$I` actor object. The only difference is how they are executed. To run tests as Symfony test you should enable corresponding module in functional suite configuration file `tests/functional.suite.yml`. Probably you want Doctrine to be included as well. Then you should use this configuration:
132124
133125
```yaml
134126
class_name: FunctionalTester
135127
modules:
136128
enabled:
137129
- Symfony:
138-
app_path: '../../app'
139-
var_path: '../../app'
130+
app_path: 'app'
131+
var_path: 'app'
140132
- Doctrine2:
141133
depends: Symfony
142-
- \AppBundle\Helper\Functional
134+
- \Helper\Functional
143135
```
144136

145137
<div class="alert alert-warning">
@@ -154,13 +146,13 @@ modules:
154146

155147
### API Tests
156148

157-
API Tests are done at functional testing level but instead of testing HTML responses on user actions, they test requests and responses via protocols like REST or SOAP. To create api tests for `ApiBundle` bundle you should create a suite for them
149+
API Tests are done at functional testing level but instead of testing HTML responses on user actions, they test requests and responses via protocols like REST or SOAP. To create api tests, you should create a suite for them:
158150

159151
```
160-
php bin/codecept g:suite api -c src/ApiBundle
152+
php bin/codecept g:suite api
161153
```
162154

163-
You will need to enable `REST`, `Symfony` and `Doctrine` module in `src/ApiBundle/tests/api.suite.yml`:
155+
You will need to enable `REST`, `Symfony` and `Doctrine` module in `tests/api.suite.yml`:
164156

165157
```yaml
166158
class_name: ApiTester
@@ -171,11 +163,11 @@ modules:
171163
depends: Symfony
172164
- Doctrine2:
173165
depends: Symfony
174-
- \ApiBundle\Helper\Api
166+
- \Helper\Api
175167
config:
176168
- Symfony:
177-
app_path: '../../app'
178-
var_path: '../../app'
169+
app_path: 'app'
170+
var_path: 'app'
179171

180172
```
181173

@@ -190,22 +182,22 @@ Symfony module actions like `amOnPage` or `see` should not be available for test
190182

191183
### Unit Testing
192184

193-
You should put your unit tests for AppBundle into `src/AppBundle/tests/unit`. It is done to not mix them with functional and API tests. To start unit test suite for `AppBundle` shoule be created:
185+
Create a unit test suite with:
194186

195187
```
196-
php bin/codecept g:suite unit -c src/AppBundle
188+
php bin/codecept g:suite unit
197189
```
198190

199-
Codeception is powered by PHPUnit so unit and integration test work in a similar manner. To genereate a plain PHPUnit test for `Foo` class inside `AppBundle` please run
191+
Codeception is powered by PHPUnit so unit and integration test work in a similar manner. To genereate a plain PHPUnit test for `Foo` class, run:
200192

201193
```
202-
php bin/codecept g:phpunit unit Foo -c src/AppBundle
194+
php bin/codecept g:phpunit unit Foo
203195
```
204196

205197
This generates a standard test inherited from `PHPUnit_Framework_TestCase`. For integration tests you may use Codeception-enhanced format which allows accessing services DI container, Doctrine, and others. You will need to enable Doctrine2 and Symfony module in `unit.suite.yml` config. Such integration test is extending `Codeception\Test\Unit` class and created by running:
206198

207199
```
208-
php bin/codecept g:test unit Foo -c src/AppBundle
200+
php bin/codecept g:test unit Foo
209201
```
210202

211203
Actions of Symfony and Doctrine2 modules will be accessible from `$this->tester` inside a test of `Codeception\Test\Unit`.
@@ -214,15 +206,3 @@ Actions of Symfony and Doctrine2 modules will be accessible from `$this->tester`
214206
<span class="glyphicon glyphicon-info-sign" aria-hidden="true"></span>
215207
Continue to <a href="http://codeception.com/docs/05-UnitTests">Unit Testing Guide &raquo;</a>.
216208
</div>
217-
218-
219-
### Running it all together
220-
221-
Codeception allows to execute all tests with different configurations in one runner. To execute all tests at once with `codecept run` you should include bundle configurations into global configuration file `codeception.yml` in the root of a project:
222-
223-
```yaml
224-
include:
225-
- src/*Bundle
226-
```
227-
228-
Then running codeception tests from root directory will execute tests from all bundles as well as acceptance tests.

0 commit comments

Comments
 (0)