Skip to content

Automated Testing

Shivam edited this page Jan 17, 2022 · 1 revision

Controllers are a central part of any ASP.NET Core API service. As such, we should have confidence they behave as intended for our application. Automated tests can provide us with this confidence and can detect errors before they reach production.

TestCase1

The test pyramid illustrates that you should have a few UI tests, many Integration tests, and even more Unit tests.

  • UI Testing — These tests ensure that proper testing coverage for business logic, corner cases and component interactions with unit and integration tests.
  • Integration Testing — These tests ensure that component interactions work as expected against external artifacts like databases and file system.
  • Unit Testing — These tests ensure that individual components of the application work as expected.

Unit Testing involves testing of small piece of functionality in a microservices. For eg. we have a CreateCategory method in our Category controller and for that we have written Handle_EmptyCategory_AddedToCategoriesRepo and Handle_CategoryLength_GreaterThan_10_AddedToCategoryRepository unit test cases to validate if category name is empty or its length is more than 10 respectively.

TestCase2

Integration testing sits in the middle of the automated test pyramid and ensures individual modules and components in our code can be successfully connected to produce the expected behavior. For the same CreateCategory method in our Category controller we have written single integration test case named Post_Category_ReturnsSuccessResult, which tests the end-to-end functionality.

TestCase3

Unit Testing

Unit testing plays a very important role in making the software more maintainable. Unit tests involve testing a part of an application in isolation from its infrastructure and dependencies. It tests one small piece of functionality in a microservice. The unit represents a unit of work – usually a single method in our code. A controller unit test avoids scenarios such as filters, routing, and model binding.

Purposes of unit testing

The purposes of unit testing are:

  • To segregate and test that each part of a program is working as expected.
  • To increase confidence while modifying logic when maintaining code.
  • To easily identify the defects in an application since unit testing data is closed to production.
  • To verify the accuracy of each program unit.

We have used xUnit Test Project template for testing. The xUnit is an open-source unit testing tool for the .NET framework that simplifies the testing process.

NuGet Packages

We need to install xunit and xunit.runner.visualstudio packages from NuGet Package Manager.

Steps to run Test Cases

There are two ways to run a test case;

  1. Right click on Test case application and click on Run Tests as shown below,

UnitTest1

  1. Select Test in toolbar then select Run All Tests to run all test cases or Test Explorer.

UnitTest2

This will display the list of test case methods. Then right-click on the GetCategoriesListTest method and click Run, and the result will be shown as below,

UnitTest4

The [Fact] attribute will be green as test case is passed. [Fact] attribute, which is used by the xUnit framework, marking them as the actual testing methods.

UnitTest6(Fact)

CI Build Result,

UnitTest7(CiBuild)

We have also integrated it with sonarCloud. Click here to sign-in(through github account credentials) to sonarCloud and go to Coverage.

UnitTest9(SonarCloudPage)

Then select particular test case as shown below,

UnitTest10(SonarCloudList)

It will show code coverage for that test case. Code coverage is 100% for this unit test case.

UnitTest8(SonarCloudCode)

Code Snippet

This is GetCategoriesListQueryHandlerTests example code,

UnitTest5(FullCode)

Demo

Demo video to get the clear result view of above implemented module.

Unit.Testing.mp4
Clone this wiki locally