There are no "rules" other than the "rules" you set for yourself.
Discuss the game with the other players!
- Make sure to declare a PHP constraint in the
composer.json
Different versions of PHP are supported by different versions of PHPUnit. Setting the PHP version makes sure a compatible PHPUnit version is required. - Add any extensions to the
composer.json
require
section To avoid tests failing because a certain extension is not installed, explicitly mention the needed extensions in thecomposer.json
file. That way whencomposer install
(or update) is run to fetch PHPUnit (and other dependencies), composer will report the missing extension. - Add PHPUnit as a development dependency (using
composer require --dev 'phpunit/phpunit'
)
To make sure the a version of PHPUnit is used that can actually run a projects tests (either because the API the tests use or the PHP version that PHPUnit requires), ship a PHPUnit with a project by declaring it as development dependency. - Use the PHPUnit shipped with a project, not a globally installed one.
- Do not mix API's from different PHPUnit versions
Write tests for a specific version of PHPUnit. - Do not commit the
phpunit.xml
file⇗
Use aphpunit.xml.dist
instead - Declare the PHPUnit version in the
phpunit.xml.dist
file.
By declaring the correctxmlns:xsi
andxsi:noNamespaceSchemaLocation
, a specific version of PHPUnit XML configuration can be specified. This makes it possible for XML enabled editors to autocomplete configuration settings. This saves time looking things up in the manual. - Use one assert per test method
- Undecided Use the static
self::assert()
rather than$this->assert()
Asserts provided by PHPUnit are all static. The abstractTestCase
class extends the abstractAssert
class. One could even go as far as usingAssert::assert()
- Add separate methods to create Mocks
When a mock is needed more than once, for the sake of readability (and to reduce code duplication) create a method that creates the mock. In extension of PHPUnit'sgetMock
function, naming a method that creates a mockFoo
should be namedgetMockFoo
. - Test structure should mirror code structure
A class that testsFoo
should be calledFooTest
and should live in the same namespace.
- Test classes should be named after the class they are testing
- Test method names should be TestDox/AgileDox compatible
- Test method names should contain the subject of the test
- Test method names should contain the expected behaviour of the test
- Test method names should contain the context of the test
Method name format: function test[Subject]Should[ExpectedBehaviour]When[GivenContext]