testing-talk/readme.rst

104 lines
1.9 KiB
ReStructuredText
Raw Normal View History

2018-06-20 21:10:52 +02:00
Testing Talk
============
Clean everything::
rm -rf composer.lock vendor web Tests
Install dependencies
--------------------
Using composer::
composer install
PHPUnit Installation
--------------------
Install phpunit::
composer require --dev phpunit/phpunit ^6.5
Why 6.x? We use 6.x to support PHP 7.0.
Check installation::
./vendor/bin/phpunit --version
Links:
* https://phpunit.de/
* https://packagist.org/packages/phpunit/phpunit
Create first test
-----------------
2018-06-24 20:24:46 +02:00
Hands on! Let's write a first basic test.
E.g. a small model with a bit logic::
2018-06-20 21:10:52 +02:00
mkdir -p Tests/Unit/Domain/Model
cp Resources/Private/CodeExamples/Tests/Unit/Domain/Model/AddressTest.php \
Tests/Unit/Domain/Model/AddressTest.php
2018-06-24 20:24:46 +02:00
Execute the first test::
2018-06-20 21:10:52 +02:00
./vendor/bin/phpunit Tests/Unit/
2018-06-23 20:18:07 +02:00
2018-06-24 20:24:46 +02:00
What's in the test?
-------------------
#. We have one PHP class `AddressTest`.
#. Two public methods.
#. The methods are annotated with `@test`.
#. Methods create an instance of the class to test.
#. Methods call an `assert*()` method.
2018-06-23 20:18:07 +02:00
Create test for controller
--------------------------
2018-06-24 20:24:46 +02:00
Introduction to mocking
^^^^^^^^^^^^^^^^^^^^^^^
Add the test
^^^^^^^^^^^^
2018-06-23 20:18:07 +02:00
We want to test the controller now::
mkdir -p Tests/Unit/Controller
cp Resources/Private/CodeExamples/Tests/Unit/Controller/FrontendUserControllerTest.php \
Tests/Unit/Controller
Execute all tests::
./vendor/bin/phpunit Tests/Unit/
2018-06-24 20:24:46 +02:00
Alternative output
------------------
testdox
Used as "agile" output::
./vendor/bin/phpunit Tests/Unit/ --testdox-html Results/testdox.html
xdg-open Results/testdox.html
xml
Used in CI to parse results::
./vendor/bin/phpunit --log-junit Results/junit.xml Tests/Unit
html Coverage
Used to check which methods still need testing::
./vendor/bin/phpunit --coverage-html Results/Coverage/ --whitelist Classes Tests/Unit
xdg-open Results/Coverage/index.html
Benefits of tests
-----------------