Here are my Top 5 Naming Patterns for Unit Tests in C#

Naming tests is an underrated part of the testing process. Here are 5 patterns that you can use to keep your test names consistent. Link to heading

Why is naming important? Link to heading

Naming tests is an important task to write maintainable code. A good naming convention allows other developers to quickly recognize the purpose of a test. Therefore, a lot of patterns have been created over the years. I’ll introduce the most common ones. You can use them or come up with your own variant. Being consistent and understandable is the key.

Given When Then Link to heading

The Given When Then pattern is used a lot in Behavior-Driven Development. It defines preconditions (Given), an action (When), and a result (Then). This leads to very long, but easily understandable test names.

▶ GivenDatabaseIsUp_WhenUserInputIsValid_ThenUserIsCreated

Here is an article that describes how to use BDD with C#

How to use Behaviour-Driven Development (BDD) with C# and SpecFlow Link to heading

Here is an introductory guide on how to use Behaviour-Driven Development (BDD) with C# and SpecFlow in a .NET application.

When Then Link to heading

The When Then pattern is similar to the Given When Then pattern but omits any preconditions. Names will be shorter, but most likely not as understandable. This is similar to the Should When approach.

▶ WhenUserInputIsValid_ThenUserIsCreated

Should Link to heading

The Should pattern gives a hint of what a test should check for. This is similar to the Verify approach.

▶ ShouldCreateUserInDatabase

Should When Link to heading

The Should When pattern indicates the outcome of a test by including a condition. It’s more verbose than the Should approach and tends to be more understandable. This is similar to the When Then approach.

▶ ShouldCreateUserInDatabaseWhenInputIsValid

Verify Link to heading

The Verify approach tells a user what the outcome of a test should be. This is similar to the Should approach.

▶ VerifyCreateUserInDatabase

Conclusion Link to heading

From my experiences, these patterns cover the majority of unit tests that I have seen so far. No matter what you choose, just be consistent. It’s confusing to find multiple naming patterns in a code base.

I tried the new C# 11 features - Here are my useful takeaways Link to heading

.NET Conf 2022 is over and Microsoft released .NET 7 together with C# 11. Here are the new features and my opinion of them.

How to fix a broken branch after a git merge or git rebase Link to heading

This guide shows you how you can create a new branch with the original changes of your broken branch.

I tried the new AI-powered Microsoft Designer — is it better than Canva? Link to heading

Microsoft Designer is currently in a preview version available for invited users. Here is my first impression of the tool so far.