Write clean tests with NFluent

NFluent logo

I believe in automated testing. Not only to check that my program works but although as specifications and documentation for my application. When an acceptance test passed you know that the feature is working as it should. Automated tests are also a very good way to document your code and your application, because if the test passed it means that the functionnality is working. And if this functionnality is updated so is the test otherwise it will fail : it is up to date !

At a code level a test let you know how to use the functions/classes you can access and what the return value will be. It is useful when using an external library in your own application. Well, this is a lot of responsibility for a test, so it has to be clear, concise and most of all understandable when read. This is not an easy task but there are some tools that can help you, one of them is NFluent.

Smooth your .NET TDD experience with NFluent! NFluent is an ergonomic assertion library which aims to fluent your .NET TDD experience (based on simple Check.That() assertion statements). NFluent aims your tests to be fluent to write (with a super-duper-happy ‘dot’ auto-completion experience), fluent to read (i.e. as close as possible to plain English. – NFluent team on GitHub.

I used this .NET library for a few months now and I must admit that I just love it ! I just can’t see myself writing unit tests without it, they are easier to write and easier to read. There are a lot of built-in functions based on the type of the value that is checked :

public class NFluentExamples
    public void NFluent_Integer_Examples()
        var sum = Calculator.Add(5, 9);
    public void NFluent_String_Examples()
        const string name = "John Doe";
        Check.That(name).IsNotEqualTo("Jane Doe");
        Check.That(name).Contains("John", "Doe");

It is also possible to chain the checks the following way  :

int? one = 1;

In my last blog post I wrote about exception testing, this is how it is done with NFluent :

public void NFluentTest()
    Check.ThatCode(() => ClassToTest.IsAQuestion(null)).Throws<NullReferenceException>();

You can find a lot more relevant examples on the project website.

Beside making you write cleaner and more readable tests, @NFluent is free, easy to install, open-source and comptatible with your favorite testing frameworks. It is hard to find an excuse for not using it !

NFluent can help you having meaningful tests that become documentation for your application and your public APIs.

See you next time !

3 thoughts on “Write clean tests with NFluent

    • Hello Janus007,

      Thank you for your interest regarding this topic.
      I created the following piece of code to answer your question:

      public void BytesArrayTest()
      byte[] bytes = {1, 2, 3, 4};

      // Order is checked when using ContainsExactly
      Check.That(bytes).ContainsExactly(1, 2, 3, 4);


      var copy = bytes.Clone() as byte[];

      // Fails because the hashcode is different


      I hope this example can help you.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s