Tests: re-organize and split large files
jrfnl opened this issue · 3 comments
What with the expansion of the test suite over the last few months, a number of test files are becoming quite large and it is not always straight forward anymore to navigate/find what's tested and where.
I'd like to propose to re-organize the test suite along the following lines:
- Each class translates to a subdirectory in the
tests
directory.
Classes in subdirectories insrc
will follow their namespace path first and then have a subdirectory for the class itself.
Examples:- The tests for the
WpOrg\Requests\Response
class would live in thetests/Response
subdirectory. - The tests for the
WpOrg\Requests\Utility\FilteredIterator
class would live in thetests/Utilities/FilteredIterator
subdirectory.
- The tests for the
- Tests which are testing a class as a whole are put in an
ClassNameTest
class in this (sub)directory. - Unit tests for specific methods each get their own test class and file, where the class and file are named after the method being tested.
For instance, tests specifically for theWpOrg\Requests\Cookie::domain_matches()
method would live in aWpOrg\Requests\Tests\Cookie\DomainMatchesTest
class.
For closely linked groups of methods, it is perfectly fine to use one test class, as long as the name of the test class is descriptive.
Example: tests for array access methods implemented in a class, could be placed in one test class, with the class being calledArrayAccessTest
.
When re-organizing the tests along the above lines, I'd also like to propose to order tests in the new classes along the following lines:
- Tests are ordered by the logic order within the method under test.
For example: if a method under test roughly does: 1) input validation, 2) something conditional 3) something unconditional.
The tests within the class would be ordered along the same lines, i.e. first the tests for the input validation, then the tests covering the conditional action, then the tests covering the unconditional action. - Data providers are closely linked to the tests they are used by in two ways:
- If the data provider is only used by one test, the name of the data provider should mirror the name of the test method, replacing
test
withdata
, i.e. thetestSomething()
method would have a data provider calleddataSomething()
. - Method order: the data provider will directly follow the test it is used for. If a data provider is used by multiple test methods, it will follow the last of these methods.
- If the data provider is only used by one test, the name of the data provider should mirror the name of the test method, replacing
- Test "helper" methods will be placed at the end of a test class and should be named descriptively with regards to their function in the tests.
@schlessera This is roughly what we discussed in one of our calls. Did I miss anything ? Anything else we should consider/create guidelines for before starting this action ?
@schlessera Mind looking this over and leaving an opinion ? If we are agreed, I can start getting this set up.
@schlessera 👆🏻 When you have a moment ?