4. Fluent Assertions

FluentAssertions

FluentAssertions (https://fluentassertions.com/) is a NuGet package that can help you to write readable assertions. Like the name suggests, it lets you write the test assertions in a fluent syntax. If we rewrite the unit test of our calculator, it will look like:

[TestClass] 
public class UnitTest1 { 

[TestMethod] 
public void TestMethod1() {
  // Arrange 
  var calculator = new Calculator(); 
  
  // Act 
  var sum = calculator.Add(1, 2); 

  // Assert 
  sum.Should().Be(3); 
 } 
} 

Note the assertion. It is an almost readable sentence. The sum should be three! The benefits of writing the assertions this way is that another, non-developer (like an analyst or tester) can read what the outcome of the test should be. There are a whole lot of methods that FluentAssertions supports.

Another benefit of FluentAssertions is that you only need to know the attributes for the test framework you are using and not the different syntaxes of the assertions.

An alternative framework to write readable assertions is Shouldly:

https://github.com/shouldly/shouldly