Unlocking the Power of Software Testing in ASP.NET: Enhancing Software Quality: xUnit & Coverlet — I

Belarmino Silva
4 min readJul 7, 2023

--

Tests and Test Coverage with xUnit and Coverlet — Part I

I will share a little bit about testing. In this article, I will cover the theoretical concepts and some specificities applied to ASP.NET applications. In the next, I will provide an example.

Testing is a topic that is not always taken into consideration, and I believe this is largely due to a lack of understanding of its benefits. When decision makers who have not yet included testing as part of their work become fully aware that it is a long-term investment, that tests can improve and enhance the quality of the delivered product, it also serves as an assurance that future changes will not compromise the normal functioning of what already works.

Performing tests also involves considering and implementing important concepts such as SOLID Principles, especially the Single Responsibility Principle, Interface Segregation Principle, and Dependency Inversion Principle. Otherwise, testing can become an inviable process.

During the presentation of GitHub Copilot at Microsoft Build, an artificial intelligence was introduced that can generate tests, which seems to be a helpful tool.

Testing Pyramid

Tests can be classified according to their level of importance and difficulty of implementation. This classification helps with explanation and demonstration. Below is a pyramid that illustrates the different levels of testing.

While other types of tests can be found in various sources, here we will consider three types of tests:

  • Unit test;
  • Integration test;
  • End-to-End tests (E2E), UI test, or Interface test;

Tests at the base of the pyramid are the simplest to create since we are dealing with independent components. However, it is necessary to perform more unit tests than E2E tests to ensure good software quality.

To keep the article from becoming too lengthy, let’s focus on the base of the pyramid, which is unit testing. We will cover test coverage later on.

Unit Tests

Unit tests allow us to test the smallest software component, which, in the context of ASP.NET applications, can be properties, methods, or classes. Unit tests ensure that the expected results are in line with the result obtained for different scenarios or given inputs.

Importance of Testing

  • Ensures that even with changes in code, library updates, or transitioning to a new version, the behavior remains consistent.
  • Enforces the implementation of concepts like SOLID Principles, which brings significant benefits, particularly in the maintenance process.
  • Enables the identification, correction, and reduction of bugs before delivering the software to the client or putting it into production.
  • Ensures that in CI/CD scenarios, publication only occurs when all tests are successful.
  • Ensures that both experienced and inexperienced developers have a correct understanding of the functionality.

For ASP.NET projects, we have several tools available for testing, such as xUnit, NUnit, or MSTest. The tool we will use for testing is xUnit.

xUnit is a free, open-source, cross-platform library that allows testing of ASP.NET applications. Created by the same team behind NUnit, it is part of the .NET Foundation and is licensed under Apache 2.

To test code with xUnit, we need to create classes with common methods and mark the methods with either [Fact] or [Theory]. In the case of the latter, multiple inputs can be provided to test the same method.

Test Coverage

Although unit tests are used to ensure the proper functioning of a specific code section, they may not cover all possibilities. To address this, we have code coverage libraries that indicate the level of test coverage for a project.

To measure test coverage, we have a library called Coverlet. It is an open-source, cross-platform library that collects data related to the tests of existing functionalities and generates a report indicating the coverage level of each unit and the project as a whole. Created by Toni Solarin-Sodara, it is a project of the .NET Foundation.

The separation of a project into layers, as suggested by Clean Architecture in the image above, allows for testing and knowing the code coverage of each layer. As a result, it becomes possible to test only the business rules or core of the application, which is the most important part to be tested, without worrying about external concerns.

Conclusion

Invest in testing, especially unit tests, not only because they form the foundation of the presented pyramid, but also because ensuring that each unit functions properly increases the chances of success in other tests.

Tests and Test Coverage with xUnit and Coverlet — Part 2

Reference

coverlet-coverage/coverlet: Cross platform code coverage for .NET (github.com)

xunit.net

Clean Coder Blog

C# — XUnit Básico (macoratti.net)

Pirâmide de Testes: o que você precisa saber sobre esse conceito (onedaytesting.com.br)

--

--

Belarmino Silva

I work as a Software Developer. I love the Java, ASP.NET ecosystem, and Data Engineering.