Unit Tests
Libraries
xUnit
xUnit.net is a free, open source, community-focused unit testing tool for the .NET Framework. Written by the original inventor of NUnit v2, xUnit.net is the latest technology for unit testing C#, F#, VB.NET and other .NET languages. xUnit.net works with ReSharper, CodeRush, TestDriven.NET and Xamarin. It is part of the .NET Foundation, and operates under their code of conduct. It is licensed under Apache 2 (an OSI approved license).
FluentAssertions
A very extensive set of extension methods that allow you to more naturally specify the expected outcome of a TDD or BDD-style unit tests. Targets .NET Framework 4.7, as well as .NET Core 2.1, .NET Core 3.0, .NET 6, .NET Standard 2.0 and 2.1.
Naming Conventions
Unit Tests should be named on the following way
// Method to test
public int Add(int a, int b)
{
return a + b;
}
// Test method
[Fact]
public void Add_ShouldReturnPositiveNumber_WhenInputsArePositive()
{
// Arrange
var a = 5;
var b = 3;
var expected = 8;
// Act
var actual = Add(a, b);
// Assert
actual.Should().BeGreaterThan(0);
actual.Should().Be(expected);
}
Last modified: 21 May 2024