DEV Community

Thibaut Andrieu
Thibaut Andrieu

Posted on • Updated on

Tiny Tips : Naming your Unit Tests

Let's assume I have a Customer class that can addProduct to basket. I have a bunch of test that test various cases. I'm not talking about any particular test framework here, they all work the same way.
For years, I used to name my test more or less like that:

TEST(CustomerTest, addProduct)
{
}

TEST(CustomerTest, addProductEmptyBasket)
{
}

TEST(CustomerTest, addNoProduct)
{
}
Enter fullscreen mode Exit fullscreen mode

Meh...

I had the chance to work with Sandro Mancuso a few years ago, which gave me the following naming convention:

TEST(CustomerShould, addProduct)
{
}

TEST(CustomerShould, addProductInEmptyBasket)
{
}

TEST(CustomerShould, doNothingWhenAddNonExistingProduct)
{
}
Enter fullscreen mode Exit fullscreen mode

Thanks to this, the intent of each test and the expected behavior is much clearer. It also fit very well with TDD. You can list what scenario should be tested even before writing test bodies.

Top comments (0)