Unit Tests allows you to make big changes to code quickly. You know it works now because you've run the tests when you make the changes you need to make, you need to get the tests working again. This saves hours.

This post not for how to write unit testing, it focuses on how to know the fail reason of your unit testing.

What Is FluentAssertions?

Fluent Assertions is a set of .NET extension methods that allow you to more naturally specify the expected outcome of a TDD or BDD-style test.

With Fluent Assertions, the assertions look beautiful, natural and most importantly, extremely readable.

Let I show you how FluentAssertions make beautiful unit testing

Let say we have a test method like this below.

public void TestMethod()
    string actual = "http://topnguyen.net";
        .And.HaveLength(9); // this is wrong

Like you see, the string "http://topnguyen.net" have 20 characters, but I expect it just has length is 9.

So this unit testing will fail and this below is the result FluentAssertions show me.

Fluent Assert Result

It so beautiful and readable, save many my time to find the reason of fail unit testing.

Easy enough, right?

You can see others sample and guide from FluentAssertions wiki

An Important NuGet Note

PM> Install-Package FluentAssertions

Github link

Wiki link


Nothing is more annoying than a unit test that fails without clearly explaining why. More than often, you need to set a breakpoint and start up the debugger to be able to figure out what went wrong. FluentAssertions has much better support for exceptions and some other stuff that improves readability and makes it easier to produce tests.

Happy Coding!