I am currently going to start from scratch with the phpunit tests for a project. So I was looking into some projects (like Zend) to see how they are doing things and how the
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
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:
tests
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.
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.
It's not going to work anyways if you have more than one test class in one file so avoid that pitfall.
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.
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.
It can be useful to create multiple xml files if you want to have:
As it is related to starting a new project with tests:
@covers
tags like described in my blog (Only for unit tests, always cover all non public functions, always use covers tags.You don't need any sort of auto loading for your tests. PHPUnit will take care of that.
Use the
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).
phpunit --filter
or phpunit tests/unit/module1
strict
mode from the get go and never turn it off.