BDD style unit test names
A couple of my colleagues have been posting about how to name your unit tests based on this original post by Jay Fields.
I think that test names are useful, especially when written in a BDD style expressing what a test is supposed to be doing.
For example, in a C# NUnit test we might see the following as a test name:
[Test]
public void ShouldDoSomething()
{
// Code testing that we're doing something
}
I write all my tests like this and I’m often asked what the point of the 'Should' is, why not just name it 'DoSomething'.
For me although it’s a subtle change it influences the way that people look at the test. They can look at the test in a much more critical way.
In the above example someone can look at the test name 'ShouldDoSomething' and then think 'Actually no, it should be doing something else.' The test name and the test contents can then be changed to fit this new understanding of the test.
I’m with Chris Johnston who says the following when it comes to naming tests:
One habit that I picked up on my last project was naming a test after I had written it. While creating the unit test, I would simply name it foo(). Then, once I saw what the code was actually doing, I would go back and rename the test to something appropriate.
Ideally I want to name the test properly before I write it but sometimes it becomes clear to me exactly what the name should be as I’m writing the test.
If I’m in this situation I usually just name the test 'ShouldDoSomething' and then by the time I’ve written it I know what it should actually be doing and I can go back and rename it.
In terms of the general argument over whether we should even have test names, I have only really coded tests in Java and C# so I’m not in a position to comment on whether using a language like Ruby means that they aren’t necessary.
For me the test name provides the intention of what you are trying to do, and I like to skim the list of tests in a class - by using Ctrl - F12 and then typing in 'Should' to filter the method list in Resharper - so that I can see at a high level what is supposed to be happening in that test fixture.
In the world of IntelliJ there is also a nice plugin called TestDox which allows you to get a list of your tests nicely indented with spaces.
About the author
I'm currently working on short form content at ClickHouse. I publish short 5 minute videos showing how to solve data problems on YouTube @LearnDataWithMark. I previously worked on graph analytics at Neo4j, where I also co-authored the O'Reilly Graph Algorithms Book with Amy Hodler.