5 helpful Nuget package for Unit Testing in .NET

Nitesh Singhal
5 min readOct 15, 2021

--

In this article, I am not going to talk about known Unit test framework or packages like NUnit or xUnit or Moq. These are well known packages and I am sure most of you are already familiar with them.

Here I am going to talk about some Nuget Packages which can help you write better test or achieving more with your existing framework like NUnit and others.

Each package listed here is for different purpose within Unit test context and can be used together.

So Let’s get started.

Fluent assertion

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 unit test.

Let’s look at example

string actual = "ABCDEFGHI";
actual.Should().StartWith("AB").And.EndWith("HI");

It gives better readability of expected outcome of your tests. In case of test failure it gives you better information as in why it has failed.

For the same example, we see result the with and without fluent assertion.

Without Fluent Assertion

Test code without Fluent assertion
Results without fluent assertion

With Fluent Assertion

Test code with Fluent Assertion
Results with Fluent assertion

The results are self explanatory, in earlier case we have to provide error text and it printed as is, where as with fluent assertion it gives real reason for failure.

you can explore more on https://fluentassertions.com/

Verifytest

Verify is a snapshot tool that simplifies the assertion of complex data models and documents.

Verify is called on the test result during the assertion phase. It serializes that result and stores it in a file that matches the test name. On the next test execution, the result is again serialized and compared to the existing file. The test will fail if the two snapshots do not match: either the change is unexpected, or the reference snapshot needs to be updated to the new result.

Let’s look at the example

Class under test
Unit test for ClassUnderTest

Initial Verification

When the test is initially run will fail with:

First verification. Tests.VerifyOrder.verified.txt not found.
Verification command has been copied to the clipboard.

To verify the result manually copy the text to the new file

Tests.VerifyOrder.verified.txt

Successive Verification

When you run the tests again, it will compare the result with verified file and will be successful if they are same.

Results after saving verified file

It is very easy to use verify tests for comparing the complex objects.

you can explore more on https://github.com/VerifyTests/Verify

Bogus

Imagine a situation where you have to test your code for 1000 Person, then manually generating 1000 persons information would be very difficult and time consuming. To be honest I would ran out names after some time.

What if there is way to generate meaningful Fake data for our application for testing.

Bogus can help us creating Fake data for testing.

Bogus is a simple and sane fake data generator for .NET. Bogus will help you load databases, UI and apps with fake data for your testing needs.

Let’s have look at one example to understand more.

we have a class called “BillingAddress”

and we want to generate 10 billing address then we have to write the code like this.

and we get following result

we get meaningful address which can be used for testing. and we save lot of time in thinking about the addresses.

you can explore more on https://github.com/bchavez/Bogus

Stryker-net

If you want to know the effectiveness of your Unit tests, Stryker-net tool can help you do it.

Stryker-net is a mutation testing tool for .Net projects. It executes your tests by temporarily inserting bugs(or Mutants) in your source code.

What is mutation testing?

Bugs, or mutants, are automatically inserted into your production code. Your tests are run for each mutant. If your tests fail then the mutant is killed. If your tests passed, the mutant survived. The higher the percentage of mutants killed, the more effective your tests are.

Stryker.NET can both be installed globally and locally.

dotnet tool install -g dotnet-stryker

Execute test

For most projects no configuration is needed. Simply run stryker and it will find your source project to mutate.

dotnet stryker

after execution of all the tests , you will see the results like this.

stryker results

Results will be stored in the directory called “StrykerOutput”

You can explore more on

https://stryker-mutator.io/docs/

https://github.com/stryker-mutator/stryker-net

Coverlet

No unit testing is complete without knowing how much your code is being covered by the Unit Tests.

Coverlet is one such library which can help us in gathering the coverage information.

Just add the reference of coverlet package in your test project and execute the test with following command

add the required nuget package

<PackageReference Include="coverlet.msbuild" Version="3.1.0">

and run the following cmd.

dotnet test --results-directory "TestResults" --logger "trx;LogFileName=results.trx" /p:CollectCoverage=true /p:CoverletOutputFormat=opencover /p:CoverletOutput="./TestResults/Coverage/"

When you execute the cmd, a coverage file “coverage.opencover.xml” will be generated in “TestResults/Coverage” directory.

Coverlet results

ReportGenerator tool can be used to generate readable report.

You can explore more on https://github.com/coverlet-coverage/coverlet

Summary

This is not the end of the list there are many such packages or tools which helps us in Unit Testing.

Let me know in comments if you also know some of them.

Happy Unit testing..!

--

--

Nitesh Singhal

Software architect, Exploring ASP.Net core and containerization technologies