Library: Shouldly and FluentAssertions

Library: Shouldly and FluentAssertions

Cleaning up the Fluent Assertions 8.0 licensing mess

Published on Wednesday, January 22, 2025

Fluent assertions

In classic test assertions, we have expected results on the left, and on the right, we have results we want to test.

Assert.Equal(12, x);

In my opinion, there is nothing logical here. We must learn this pattern and remember that left means expected and right means actual.

The problem many of us have, is that sometimes we switch these two positions.

It is even more confusing if we have to test if something is greater or less than something else.

Assert.Greater(12, x);

This breaks my brain even more. I'm not the only one! For example, xUnit and MSTest do not have Is.LessThan and Is.GreaterThan.

On the other hand, the fluent-style assertions are much easier to follow (for our human brains):

x.Should().Be(12);
x.Should().BeLessThan(12);

This approach is logical and easy to follow. There are no rules to remember. We just read from left to right.

Even for much more complex cases, even if you are not a developer, you can have some understanding of what is going on:

string actual = "ABCDEFGHI";

actual.ShouldStartWith("AB");
actual.ShouldEndWith("HI");
actual.ShouldContain("EF");
actual.Length.ShouldBe(9);

Library: Fluent Assertions

Fluent Assertions is (probably) the oldest and the most used fluent style assertion open-source library for .NET.

I have used it with xUnit on all my projects, wherever I had any tests.

However, with Version 8, Fluent Assertions became an Xceed Partner, which now requires a paid license for all commercial projects. I'm okay with paying for good tools that help me with my projects, but the Standard License for one person is $129.95. You can still use the 7.x.x for free, even in commercial projects.

When I heard the news, at first, I thought that maybe I was not using the Fluent Assertions properly. If it costs that much, I must be doing something wrong. But I am using it "correctly." It is just an optional fluent-style assertion library.

It just does not make sense to pay this much for this type of library, which many would find non-essential. It makes much less sense because a popular open-source .NET library, Shouldy, has similar capabilities.

Even from an hourly rate perspective, how much does it cost to move to Shouldy (or even revert to classic assertions)? We can use "search and replace" or instruct LLM to convert it for most assertions. It took me over an hour to convert 1000+ tests to Shouldy.

Library: Shouldly

Shouldly is an open-source assertion framework for .NET.

The sintax is similar to FluentAssertions

  • FluentAssertions - x.Should().Be(12);
  • Shouldly - x.ShouldBe(12);

In Shouldy, you cannot chain assertions, like in FluentAssertion

string actual = "ABCDEFGHI";
actual.Should().StartWith("AB").And.EndWith("HI").And.Contain("EF").And.HaveLength(9);

But I don't think we should chain assertions at all.

Conclusions

Do not overthink. Just move to Shouldy if you are using FluentAssertions. If you are using classic assertions, try Shouldy, which will enable you to write simple, easy-to-read tests.