I recently came up with the following convention for naming my tests, their classes and containing projects in order to maximize their descriptivenes:
Lets say I am testing the Settings class in a project in the MyApp.Serialization namespace.
First I will create a test project with the MyApp.Serialization.Tests namespace.
Within this project and of course the namespace I will create a class called IfSettings (saved as IfSettings.cs).
Lets say I am testing the SaveStrings() method. -> I will name the test CanSaveStrings().
When I run this test it will show the following heading:
MyApp.Serialization.Tests.IfSettings.CanSaveStrings
I think this tells me very well, what it is testing.
Of course it is usefull that in English the noun "Tests" is the same as the verb "tests".
There is no limit to your creativity in naming the tests, so that we get full sentence headings for them.
Usually the Testnames will have to start with a verb.
Examples include:
- Detects (e.g. DetectsInvalidUserInput)
- Throws (e.g. ThrowsOnNotFound)
- Will (e.g. WillCloseTheDatabaseAfterTheTransaction)
etc.
Another option is to use "that" instead of "if".
The latter saves me keystrokes though and describes more exactly what I am doing, since I don't know, that the tested behavior is present, but am testing if it is.
[Edit]
After using above naming convention for a little longer now, I have found, that the If prefix can be confusing, when working with interfaces. It just so happens, that the testing class IfSerializer.cs looks very similar to the interface ISerializer.cs in the "Open Files Tab".
This can get very annoying when switching back and forth between the tests, the class being tested and its interface. As a result I would now choose That over If as a prefix.
Additionally I now use - only for methods in my test classes as it is not considered best practice anywhere else - the "_" to separate words in my test method names as in:
- [Test] public void detects_invalid_User_Input() *
I find this to be easier to read.
[End Edit]
I hope this spawns some more ideas, since I consider naming tests of great importance as it can save you a lot of time that would otherwise have been spent trying to understand what the tests are doing (e.g. after resuming a project after an extended hiatus).