Tools o improve your unit test with .NET
Tags: testing, testdev, dotnet, csharp
Published: 2021-06-18 11:56:06


![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mpevyiez0pljycytlb2c.jpg)

In this article, I will show you some kind tools for you can write unit test better and make you job easier. In this case, I'm using here the Dotnet platform C# language. But first, let's understand some concepts.

**Test stuntmen**

First, what are test stuntmen? They are fake implementation the object real can be simulated the unit real or the object real.


*Mock*
Are what we are talking about here: objects pre-programmed with expectations which form a specification of the calls they are expected to receive.

*Stubs*
Provide canned answers to calls made during the test, usually not responding at all to anything outside what's programmed in for the test.

*Fakes*
Are objects actually have working implementations, but usually take some shortcut that makes them not suitable for production (an in-memory database is a good example).

*Spies*
Are stubs that also record some information based on how they were called. One form of this might be an email service that records how many messages it was sent.

*Dummy*
Are objects are passed around but never actually used. Usually, they are just used to fill parameter lists.
Fake objects actually have working implementations, but usually take some.


**XUnit Code Snippets**

If you use these "code snippets", you can save time coding/typing to create unit test code based on xUnit framework. You can use write the command below I will show two options.

You can download the Snippets in your Visual Studio

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k2y12nwgjzykatks4ruj.jpg)


Use command below
1.xtestm [Tab] or
2.fact [Tab]

Then, this snippet expanded following C# code.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ara4anftib076ghy0vug.jpg)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/65ldlx5z6reo7fuce9gc.jpg)


**NBuilder**

Is a library that allows you to rapidly create test data, automatically. Some examples below.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6twiaf3lwkgxdvdulsj4.jpg)


![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6im0vn0anbmsdd93ww36.jpg)

Its a great idea and helps facilitates the process if you need to generate a quantity of data for testing.




**Faker.NET**

This library is different the NBuilder, because if you want something which looks like actual names, address, email, telephone, etc you can use Faker.NET. 


![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jkaqpveukl464ev9skkx.jpg)


**Coverlet**

Coverlet is a cross platform code coverage framework for .NET, with support for line, branch and method coverage. It works with .NET Framework on Windows and .NET Core on all supported platforms.

Coverlet documentation reflect the current repository state of the features, not the released ones.
Check the changelog to understand if the documented feature you want to use has been officially released. You can see for more details in [Coverlet Github](https://github.com/coverlet-coverage/coverlet)

Let´s first install the package coverlet in your test project in image below.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vwmhbh6uas5xj0gvyhqe.jpg)

Now we can configure the tests to execute, for to generate the XML file format, for code coverage with the command and image below.

**dotnet test --verbosity minimal --collect:"XPlat Code Coverage"**


![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/51srxsv3uk2e7bnmw7qd.jpg)

The option --collect:”XPlat Code Coverage”  will create the file **coverage.cobertura.xml** in a folder named identified by a GUID and located in the TestResults directory of the test project.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9z704p4ea3p5u27jl4ca.jpg)

Install Report Generator the command below.

**dotnet tool install --global dotnet-reportgenerator-globaltool --version 4.8.6**

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/va8d6lyop9e482et05cb.jpg)

In the power shell open the folder where the xml file was generated, and write the command **reportgenerator "-reports:coverage.cobertura.xml" "-targetdir:coveragereport" -reporttypes:Html**

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lme77zrv3oqvapsc4j6f.jpg)


![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wp0j1vvxmyperapxwmcm.jpg)

As you can see, the report generator created files in which it shows the test coverage of your code.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ff7y7jdqwia6w8cixyto.jpg)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wdskw0lx450yuj7f68t7.jpg)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/181siwutiyb6mswc8cjz.jpg)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tuwz8z2kml83k5q4sn5f.jpg)



**Conclusion**

Whenever I can, I write an article to document my programs and processes so I don't forget. Here I have put some tools that I use in my personal projects and at work to improve the way I write my unit tests, and observe test coverage. I hope you enjoyed it and it helps you.




**References**

[Martin Fowler - article MocksArentStubs](https://martinfowler.com/articles/mocksArentStubs.html)
[Documentation dotnet core unit testing code](https://docs.microsoft.com/pt-br/dotnet/core/testing/unit-testing-code-coverage?tabs=windows)
[Jerrie Pelser](https://www.jerriepelser.com/blog/creating-test-data-with-nbuilder-and-faker/)
[Coverlate](https://github.com/coverlet-coverage/coverlet)



**Links**
[Unit Test project in Github](https://github.com/JessicaNathany/unit-test)
[Mock4 Quickstart](https://github.com/moq/moq4/wiki/Quickstart)
[NBuilder GitHub](https://github.com/nbuilder/nbuilder)
[Faker.NET](https://www.nuget.org/packages/Faker.Net)
[NBuilder documentation](https://nbuilder.wordpress.com/documentation/)
[XUnitCodeSnippets](https://marketplace.visualstudio.com/items?
itemName=jsakamoto.xUnitCodeSnippets)
[Link package Moq](https://www.nuget.org/packages/moq/)
[coverlet.msbuild](https://www.nuget.org/packages/coverlet.msbuild/)
[coverlet-coverage](https://github.com/coverlet-coverage/coverlet)