Today I'm not posting a 1000-word, SEO-rich article. I'm reposting a LinkedIn post with 6 quick tips to write better test names.
Often, we don't put too much care into our unit tests, maybe with the excuse that "we don't ship tests to our customers" or "writing tests will take more time, and we don't have time". But, unit tests are our safety-net, our seat belt. They're there to give us confidence to change things.
Here you have 6 quick tips/ideas to write better names for your unit tests.
✔️ Choose a naming convention for your test names and stick to it.
✔️ Every test name should tell the scenario under test and the expected result. Don't worry about long test names. But, don't name your tests:
Test2 and so on.
✔️ Describe in your test names what your testing in a language easy to understand even for non-programmers.
❌ Don't prefix your test names with "Test". If you're using a testing framework that doesn't need keywords in your test names, don't do that. In C# with MSTest, we have attributes like
[TestMethod] to mark classes and methods as tests. Other testing frameworks have similar attributes.
❌ Don't use filler words like "Success" or "IsCorrect" in test names. Instead, tell what "success" and "correct" means for that test. Is it a successful test because it doesn't throw exceptions? because it returns a value? Make your test names easy to understand.
❌ Don't use assertion messages. Write better test names.
Voilà! Six tips as promised. If you want to dig deeper into unit testing, I wrote a post on naming conventions for our unit tests and other unit testing best practices. You will find more tips on names, organization and mocks.