Unit Testing Naming Conventions
17 Jun 2016Test Naming Conventions
When working with open source projects, you run across a few different ways to name your unit tests.
Let’s take a look at a few of them by example testing a simple calculator with one operation: Divide
public class Calculator
{
public void Divide(int x, int y) => x / y;
}
Test Method Naming Conventions
With/Will
DivideWithDivisorEqualZeroWillThrowDivideByZeroException()
Given/Should
DivideGivenAZeroDivisorShouldThrowDivideByZeroException()
Given/When/Then
GivenAZeroDivisor_WhenDivideIsCalled_ThenDivideByZeroExceptionIsThrown.
Fixture naming conventions
Test class per method
public class CalculatorTests
{
public class Divide
{
[Fact] DivideWithZeroDivisorThrowsDivideByZeroException() { /* ... */ }
}
}
When - Should fixture
public class WhenDividingByZero
{
[Fact] ShouldThrowDivideByZeroException() { /**/ }
}
Hopefully I can expand upon this list when a few more examples come to mind!