Test naming tips
No naming scheme is going to guarantee good names, so here are a few intentionally vague test naming guidelines:
Should X or Y is probably testing two branches, which
should be two tests. A sure sign of this is the use of
in the test, which should just be outlawed. I can’t think of a single case
where it would be better to have a branch in a test than two tests,
but I’d be interested to hear of any.
Should X and Y can be caused by many things. Sometimes
the test is fine, there just isn’t a single collective term for what the code
is doing. Other times the test is verifying more than one thing at a time,
such as the return value and a side effect. Unless the test is extremely slow
this should be split into two tests.
The name should say something about either a side effect or a return value. As
should parse input does neither. If the parsed
result is stored or returned, it should say so. If the test is actually
checking that nothing is thrown when parsing valid input then that should be
part of the name.
When testing errors the name should be more descriptive than
should fail when …. There are many ways code can fail, and many
ways the surrounding code can react to that failure, so a better name would be
should return error code 5 when … or
should throw not found exception when ….
Context is everything. If the test filename contains “foo” it is probably not useful to also add “foo” to any of the test names. Similarly, if you have several test names which share a context it might be useful to split those out into something with a name corresponding to that context and removing that now redundant part from the test names. As a simple example, you might want to split the “app” tests into “view” and “model” tests. After doing so you can remove “view” from the view test names and “model” from the model test names.
If it’s really difficult to come up with a good name for a test I’ve found that it’s often because I don’t have a sufficiently clear idea of what the test is meant to assert. At this point it might be useful to step back and see whether it’s possible to split up the task some more to reach some easily testable next step.
No webmentions were found.