I don't have time to write tests because I am too busy
Some people are confused about what type of tests they are practicing.
Explaination on what the different types of tests cover (acceptance => as a user)
Video of the doors.
Test pyramid, emphasising on costs of the tests, and therefore when it should be testing using which strategy.
For legacy rewriting, acceptance testing should probably be the best type of tests to choose, as it allows to confirm the code is iso in terms of features.
- test use cases, not methods, and this should be visible by reading the test signature
- tests can be documentation
A list of best practices in code writing is available in the slides.
A test is consistent, the execution is the same each time it runs. If not, that means that the test are worthless (meaning the positive result in CI does not guarantee a positive result on another environement).
Tests must be easy to read an provide the feature/intent at first sight. Naming is particularly important for tests as it is responsible in part for the self-desciption.
Tests must no contain any logic at all (loops or conditions).
Choose assertions carefully (assertSame
- ===
- is better than assertEquals
- ==
in a strongly typed code).
Failed tests messages will be more descriptive if the right assertion is chosen.
Simulate the class to inject (ie: provide return value for each method of the interface/class).
Basically test whether with the same input we get the same output.
Just like a stub + expectation. Expectation would be testing the behaviour of the methods (ie. number of time it's called and so on).
Use expectations only when necessary, as expectations would need more maintenance time generally.
ADD AN EXAMPLE OF GOOD CODE COVERAGE WITH BAD TESTS
Good metric to see if paths are covered, but not the ultimate metric as some of the use cases may be left out.
Alternative approach to PHPUnit, BDD oriented. It is an opinionated framework, so comes with limitations to guide developers to produce better tests.