I'll start of by linking to the manual and then going into what I've seen and heard in the field.
Organizing phpunit test suites
Module / Test folder organization in the file system
My recommended approach is combining the file system with an xml config.
tests/
unit/
| - module1
| - module2
- integration/
- functional/
with a phpunit.xml
with a simple:
<testsuites>
<testsuite name="My whole project">
<directory>tests</directory>
</testsuite>
</testsuites>
you can split the testsuites if you want to but thats a project to project choice.
Running phpunit
will then execute ALL tests and running phpunit tests/unit/module1
will run all tests of module1.
Organization of the "unit" folder
The most common approach here is to mirror your source/
directory structure in your tests/unit/
folder structure.
You have one TestClass per ProductionClass anyways so it's a good approach in my book.
In file organization
It's not going to work anyways if you have more than one test class in one file so avoid that pitfall.
- Don't have a test namespace
It just makes writing the test more verbose as you need an additional use statement so I'd say the testClass should go in the same namespace as the production class but that is nothing PHPUnit forces you to do. I've just found it to be easier with no drawbacks.
Executing only a few tests
For example phpunit --filter Factory
executes all FactoryTests while phpunit tests/unit/logger/
executes everything logging related.
You can use @group
tags for something like issue numbers, stories or something but for "modules" I'd use the folder layout.
Multiple xml files
It can be useful to create multiple xml files if you want to have:
- one without code coverage
- one just for the unit tests (but not for the functional or integration or long running tests)
- other common "filter" cases
- PHPBB3 for example does that for
their phpunit.xmls
Code coverage for your tests
As it is related to starting a new project with tests:
- My suggestion is to use
@covers
tags like described in my blog (Only for unit tests, always cover all non public functions, always use covers tags.
- Don't generate coverage for your integration tests. It gives you a false sense of security.
- Always use whitelisting to include all of your production code so the numbers don't lie to you!
Autoloading and bootstrapping your tests
You don't need any sort of auto loading for your tests. PHPUnit will take care of that.
Use the <phpunit bootstrap="file">
attribute to specify your test bootstrap. tests/bootstrap.php
is a nice place to put it. There you can set up your applications autoloader and so on (or call your applications bootstrap for that matter).
Summary
- Use the xml configuration for pretty much everything
- Seperate unit and integration tests
- Your unit test folders should mirror your applications folder structure
- To only execute specif tests use
phpunit --filter
or phpunit tests/unit/module1
- Use the
strict
mode from the get go and never turn it off.
Sample projects to look at
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…