Most developers who work with PHPUnit on a daily basis know the basics inside out: assertSame(), #[Before], and perhaps a few data providers. But PHPUnit can do so much more. Under the hood, there are functions, options, and workflows that even experienced professionals have often never seen, let alone used.
In my presentation "So you think you know PHPUnit", I show how to discover and use these lesser-known features. These are not exotic gimmicks, but features that make your test code more robust, meaningful and maintainable. They help you get even more out of PHPUnit every day.
In this article, you will find the most important insights from the presentation: summarised concisely and ready to apply immediately.
-
Use the names of your tests to concisely and precisely describe the aspect of your software that they cover.
Automated tests can do more than just verify whether your software works correctly. They can also serve as an executable specification and documentation of your software. The TestDox output should be understandable to your domain experts.
-
Use the
--check-php-configurationCLI option to ensure that your PHP interpreter is properly configured for development and testing with PHPUnit. -
Use the
--generate-configurationCLI option to generate an XML configuration file for PHPUnit for your project that uses best practice defaults. -
Use attributes such as
#[CoversClass]to document the target a test case class intends to cover.PHPUnit uses this information, for example, to make reported code coverage information more precise and support test selection using the
--coversCLI option. -
Use the
#[Small],#[Medium], and#[Large]attributes to document the scope of a test case class.PHPUnit uses this information, for example, to make reported code coverage information more precise and support test selection using the
--groupCLI option.
Finally, I would like to point out that despite its many features, PHPUnit is only one part of a successful testing strategy. You can find more interesting topics related to PHP development and modern testing methods in my other articles and presentations.
If you need assistance with implementing or optimising your tests, I also offer training and consulting services. Feel free to contact me so that we can work together to take your testing processes to the next level.