8th Aug 2022

The Advantages of Unit Testing with .NET Core

.NET

Written By, Jalpesh Nakum

Overview

Hello Devs,
As we know that, with the growth of different types of technology and features, all developers and organizations are working on improving their quality of code, so for that developers are moving to automated testing of their code, because they are trying to catch the bugs before they publish or release their respective projects and code.

It’s definitely true but there is nothing that’s absolutely correct and even though you are well organized with your process at the end there’ll always be something missing that would result in error. So now what to do for automated testing ?

We have many types of automated testing but it depends on our project as there are numerous aspects to be considered. In this blog I will explain about Unit Testing and how it can affect your efficiency in eliminating the bugs as well as unwanted errors.

What is the purpose of Unit Testing ?

As the name suggests UNIT means small component, we self-test our methods one by one with test cases. For unit testing we must have a list of test cases, pass each test case one by one and test our code. Purpose of unit testing is to simply test our code before we publish or release, it is used for creating code self tested.

Advantages of Unit Testing

  • Not for Unit Testing only but any kind of code testing method gives positive confidence in our code.
  • Unit testing has the ability to find problems or validation errors before publishing our code on server or live site.
  • We can be debugging our unit testing function so easily we can find errors.
  • Improve Quality of code.
  • Reduce the code of bugs fixes.

How to apply Unit Testing in .NET core projects with Blazor ?

Here, we take a simple example of conversion of celsius to fahrenheit with a unit testing method. So, let’s start with the points below one by one.
Step 1: Create Blazor project (Blazor server/ Blazor Webassembly)

  • So, here I will select Blazor serverApp for creating a demo project. We can also select Blazor WebAssembly or any other front-end project.
  • Here, we go with .NET 6 latest framework and create our Blazor Server project.

Advantage-Of-Unit-Testing-Create-Project

Step 2: Create New Component for conversion

  • Create a new component for converting celsius to fahrenheit, here we will give the name CalculateFahrenheit.
  • Right click on Pages >> Add >> Razor Component.
  • Now we will write some code for Calculate Fahrenheit from a given celsius, here is the code of our component.

Step 3: Create project for Unit test

    • Right click on our solution and as below add the new project MSTest Test Project with meaning full name.

Advantage-Of-Unit-Testing-Create-Project-for-unit-testing

    • Now, we will write our test function and test cases, here is the code of our UnitTest class.

Step 4: Build and Run project

    • When you run the application without running unit-test it will work properly and we will get correct results as below.

advantage-of-unit-testing-build-run-project

    • So, now the question arises how unit-tests are helpful for us ? Because without running it worked properly.
    • Let’s take an example as above in our calculation method that we wrote

tf = tc * 9 / 5 + 32;

    • We change in our calculation method as below and re-run the project

tf = tc * (9 / 5) + 32;

    • As below we run successfully not getting any kind of error but get wrong output because we run application without testing our code.

advantage-of-unit-testing-build-run-project-2

  • So, that time unit-test is useful to test our code before publishing it.

Step 5: How to Execute UnitTest

    • As above we already wrote code of our unit-test method with test cases, So now let’s check it.
    • For open TestExplorer Go to >> Test >> Test Explorer
    • We can show that our all test methods are inside Test explorer.
    • First Test we are passing the wrong value in our test cases like Celsius = 22, expectedResult = 70, As We know, the actual result is 71.6.
    • Now to run our test case, select our test case method and when we click the run button we get below error.

    • Here both actual and expected value are not same so it will show error as both are not same.
    • Now, changes in expected results pass the actual result we want. It means Celsius = 22, expectedResult = 71.6 and again run test cases.

advantage-of-unit-testing-execute-unit-test-2

  • So, as above if actual result and expected result both are same then our test method runs successfully else it will throw an error.
Hire Our Worthy Web Developers

Explore the Benefits of Our Web
Development Services

Get Expert Assistance

Conclusion

So, here we take a little example of how we create unit test methods and how we apply Unit testing in .NET Core projects. We must create a separate Unit method for testing, one by one passing all test cases before publishing our code and running our test cases, So we will build confidence in our code. I hope you get some idea about Unit Testing and how we apply in our projects.

Written By,

.NET Developer at Yudiz Solutions Ltd