Commit 8b37d2c1 by Ragazzo Committed by Alexander Makarov

Docs about testing

parent da179e3b
Testing environments setup
==========================
> Note: This section is under development.
By default Yii2 is bundled with [`Codeception`](https://github.com/Codeception/Codeception) testing framework
that allows you to write unit tests as functional and acceptance tests for UI too. You can also use other testing frameworks
and third party libs like: [Phpunit](https://github.com/sebastianbergmann/phpunit/), [Behat](https://github.com/Behat/Behat), [AspectMock](https://github.com/Codeception/AspectMock), [Mockery](https://github.com/padraic/mockery) and so on.
## Codeception
Codeception testing support provided by Yii includes:
- [Unit testing](test-unit.md) - verifies that a single unit of code is working as expected;
- [Functional testing](test-functional.md) - verifies scenarios from a user's perspective via browser emulation;
- [Acceptance testing](test-acceptance.md) - verifies scenarios from a user's perspective in a browser.
Yii provides ready to use test sets for all three testing types in both [`yii2-basic`](https://github.com/yiisoft/yii2/tree/master/apps/basic) and [`yii2-advanced`](https://github.com/yiisoft/yii2/tree/master/apps/advanced) application templates.
In order to run tests with Yii you need to install [Codeception](https://github.com/Codeception/Codeception). A good way to install it is
the following:
```
composer global require "codeception/codeception=2.0.*"
composer global require "codeception/specify=*"
composer global require "codeception/verify=*"
```
If you've never used Composer for global packages run `composer global status`. It should output:
```
Changed current directory to <directory>
```
Then add `<directory>/vendor/bin` to you `PATH` environment variable. Now we're able to use `codecept` from command
line globally.
## Phpunit
TBD
## Behat
TBD
Testing
=======
> Note: This section is under development.
TODO:
- https://github.com/yiisoft/yii2/blob/master/extensions/codeception/README.md
Testing is an important part of software development. Whether we are aware of it or not, we conduct testing continuously.
For example, when we write a class in PHP, we may debug it step by step or simply use echo or die statements to verify
that implementation is correct. In case of web application we're entering some test data in forms to ensure the page
......@@ -14,31 +8,56 @@ interacts with us as expected. The testing process could be automated so that ea
we just need to call up the code that perform testing for us. This is known as automated testing, which is the main topic
of testing chapters.
The testing support provided by Yii includes:
[Test-Driven Development (TDD)](http://en.wikipedia.org/wiki/Test-driven_development) is an approach of developing software when you write your code in repeatable steps that verifies produced features
with needed assertions.
Test - block of program that verifies correct or not correct behavior of needed feature. All tests should be automated to be able to make them
run often and fast.
Main life cycle of this approach contains following steps:
- Create a new test that covers a feature to be implemented. The test is expected to fail at its first execution because the feature has yet to be implemented;
- Run test and make sure the new test fails;
- Write simple code to make the new test pass;
- Run all tests and make sure they all pass;
- Refactor the code in needed way and make sure the tests still pass.
Repeat step 1 to 5 to push forward the functionality implementation. Depending on your skills you can increase or decrease size
of code that you change between steps and tests run. If you feel that you are doing a lot of simple changes and loosing your time then it is
time to make steps bigger, but if you started to spend a lot of time on debug when writing tests it is a good sign to make steps slower.
Mainly you should write code only before test to be able to understand what it does and to avoid leaks of code implementation into tests. This approach
is called `Test First`. It is a vital part of testing to understand that code implementation should not leak to your tests, thus you will
be able to concentrate more on abstractions of code and features, rather then particular implementation. Tests that have such leaks becoming
hard to maintain and understand, and as a conclusion they are deleted. However depending on your understanding of TDD and your skills of writing good decoupled code you can write tests after
the implementation is done, there is nothing wrong about it, while you are understanding what you are doing and why.
There are a lot of reasons why you should use TDD, below are the common ones:
- [Unit testing](test-unit.md) - verifies that a single unit of code is working as expected.
- [Functional testing](test-functional.md) - verifies scenarios from a user's perspective via browser emulation.
- [Acceptance testing](test-acceptance.md) - verifies scenarios from a user's perspective in a browser.
- it saves your time on debug in long term;
- it keeps you concentrated only on one thing - currently developing feature, and not in whole project. Thus you only write code that you really need;
- it lets you know immediately if something will go wrong or functionality of some features will be broken;
- it lets you understand the use case of feature you are developing now and you are able to build systems with clear interfaces;
- it lets you understand dependencies between code and how to solve them to make system decoupled, particular with [Inversion Of Control](http://en.wikipedia.org/wiki/Inversion_of_control) principle.
Yii provides ready to use test sets for all three testing types in both basic and advanced application templates.
TDD can help you to build good decoupled systems, but it will not replace some basic knowledges of refactoring and structuring code, so you
should not consider it as a `silver bullet`. It is almost always that you will think about code, features and specifications before writing any test case.
There are other software approaches to help you with it, like : [Behavior Driven Development (BDD)](http://en.wikipedia.org/wiki/Behavior-driven_development) and [Domain Driven Development (DDD)](https://en.wikipedia.org/wiki/Domain-driven_design).
Test environment setup
----------------------
However it is not always necessary to use TDD in your project, you should consider below cases of when you can or should use it:
In order to run tests with Yii you need to install [Codeception](http://codeception.com/). A good way to install it is
the following:
- you are working on big project or project that gets bigger;
- you are working on long term project;
- you have time for TDD, since tests should not be written once and forgotten, they should be also crafted as other code;
- you have correct environment that allows you to use TDD. It can be testing frameworks, fixtures frameworks and so on;
- you are working on project with legacy code that should be rewritten fully or partial;
- your team understand the value of bringing good quality code to business.
```
composer global require "codeception/codeception=2.0.*"
composer global require "codeception/specify=*"
composer global require "codeception/verify=*"
```
If you are working on simple CRUD or small projects usually you dont need TDD, since it can take a half of all time in developing with small projects.
When working on legacy code it is very hard to apply TDD due to hard system coupling and wrong architectural design issues, however by isolating some code blocks
and working with them one by one in small parts you can rewrite it much faster with TDD than without it, since you will be sure everything is works correctly and you can move on.
If you've never used Composer for global packages run `composer global status`. It should output:
Below are listed books where you can find more answers for your TDD quesitons:
```
Changed current directory to <directory>
```
- "Test-driven Development by Example", Kent Beck;
Then add `<directory>/vendor/bin` to you `PATH` environment variable. Now we're able to use `codecept` from command
line globally.
To know what testing frameworks and environments is bundled or supported by Yii2 you can see [Test Environment Setup](test-environment-setup.md) guide.
Advanced application testing environment setup
===========================================
TBD
Basic application testing environment setup
===========================================
TBD
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment