DPC2020: My Top 10 PHPUnit Tips & Tricks - Juliette Reinders Folmer

Discover top PHPUnit tips and tricks from Juliette Reinders Folmer, covering configuration, assertions, test data, code coverage, and more, to write effective and maintainable tests for your PHP code.

Key takeaways
  • Start with a simple configuration for PHP Unit, including a whitelist and specifying all source files.
  • Limit assertions per test to keep code simple and readable.
  • Use test data that is easy to generate and maintain.
  • Evaluate code coverage to ensure tests are effective.
  • Use named test cases for complex data providers.
  • Prioritize writing tests for the happy path first.
  • Use anticipatory assertions to prepare for test failures.
  • Document test methods to explain what they test.
  • Run tests with verbose mode to track changes.
  • Use code coverage to identify which parts of the code need improvement.
  • Do not use strict types in tests.
  • Avoid using own code to generate test data.
  • Write a test method for every use case in your code.
  • Consider using a data provider to test for invalid input.
  • Consider testing private methods if they are complex or essential to the code.
  • Test test cases to ensure they run correctly.
  • Regularly review test code to ensure it remains maintainable.
  • Avoid duplicate tests and ensure every test has a unique set of assertions.
  • Monitor test coverage to ensure it is not decreasing over time.
  • Run tests with code coverage to identify areas for improvement.