This section lists a few good practices to keep your tests organized and easy to maintain. The tips concern file organization, unit tests, and functional tests.
As for the file structure, you should name the unit test files using the class they are supposed to test, and name the functional test files using the module or the scenario they are supposed to test. See Listing 15-29 for an example. Your test/
directory will soon contain a lot of files, and finding a test might prove difficult in the long run if you don't follow these guidelines.
Listing 15-29 - Example File Naming Practice
test/ unit/ myFunctionTest.php mySecondFunctionTest.php foo/ barTest.php functional/ frontend/ myModuleActionsTest.php myScenarioTest.php backend/ myOtherScenarioTest.php
For unit tests, a good practice is to group the tests by function or method, and start each test group with a diag()
call. The messages of each unit test should contain the name of the function or method tested, followed by a verb and a property, so that the test output looks like a sentence describing a property of the object. Listing 15-30 shows an example.
Listing 15-30 - Example Unit Test Naming Practice
// srttolower()
$t->diag('strtolower()');
$t->isa_ok(strtolower('Foo'), 'string', 'strtolower() returns a string');
$t->is(strtolower('FOO'), 'foo', 'strtolower() transforms the input to lowercase');
# strtolower()
ok 1 - strtolower() returns a string
ok 2 - strtolower() transforms the input to lowercase
Functional tests should be grouped by page and start with a request. Listing 15-31 illustrates this practice.
Listing 15-31 - Example Functional Test Naming Practice
$browser->
get('/foobar/index')->
isStatusCode(200)->
isRequestParameter('module', 'foobar')->
isRequestParameter('action', 'index')->
checkResponseElement('body', '/foobar/')
;
# get /comment/index
ok 1 - status code is 200
ok 2 - request parameter module is foobar
ok 3 - request parameter action is index
ok 4 - response selector body matches regex /foobar/
If you follow this convention, the output of your test will be clean enough to use as a developer documentation of your project — enough so in some cases to make actual documentation useless.