How to write Unit Tests for a .NET Core Application

Read full article on the Infragistics blog

Writing unit tests is a good software engineering practice. In this article, we will learn how to create a C# .NET Core application while adhering to the Test Driven Development (TDD) approach. In the TDD approach, before implementing a functionality, you write a unit test for it. For example, if you want to write a function to add two numbers, first you’ll write a failed unit test and then implement the functionality to pass the test.

In this post, we’ll create a Calculator Application. To start, let’s follow the folder structure as outlined below:

· CalculatorApp – Solution

· CalculatorApp.Services – Application project

· CalculatorApp.Services.Tests – Test project

You can use Visual Studio to create projects; however, in this post, I will use .NET Core commands to create my projects, add tests, and run tests. You can also use MSTest or NUnit to write unit tests, but in this example, I’ll use xUnit and dotnet test to write and run my Unit Tests.

Let’s start by opening a command shell and follow along:

 Creating the Application project

To start, you’ll need to create a directory called CalculatorApp. Keep in mind that you can follow any naming convention you’d like for directories, but if you’d like to follow along with this post, I would recommend following the same directory name. So, let’s create the CalculatorApp directory and navigate to it.

Inside the CalculatorApp directory, we are going to create a new solution by running the following command:

dotnet new sln

After successfully running the command, you should get the message “The Template Solution File was created successfully”. Also inside the CalculatorApp directory, you will find a file named calculatorapp.sln.

Next, create a directory called CalculatorApp.Services. This directory will keep the application sources i.e. all classes of the calculator application. Navigate to the Calculatorapp.Services directory and create a class library by running the following command:

dotnet new classlib

After successfully running this command, you should get the message “The Template Class Library was created successfully”. You’ll also find a file named calculatorapp.services.csproj inside the CalculatorApp.Services directory. Next, you need to add this class library project to the calculatorapp solution. To do this, navigate back to the calculatorapp directory, and run the command:

dotnet sln add .\calculatorapp.services\calculatorapp.services.csproj

After successfully running the command, you should get the message “Project added to the solution”.

Inside the CalculatorApp.Services folder, you will find a class Class1 – rename it to Calculator and modify the class as shown in the listing below:

Calculator.cs

You’ll notice in the above listing that the Add and Sub functions are not implemented. First, we will write a unit test and then implement these functionalities.

Creating the Test project

To add test projects, create a directory called CalculatorApp.Services.Tests and navigate to the directory. In this directory, we will create an MS Test project by running the following command:

dotnet new mstest

This command creates the test project which uses MS Test as the test library. Once the test project is created, add the source project library in the test project. To do that, run the command:

dotnet add reference ../CalculatorApp.Services/CalculatorApp.Services.csproj

This command will add a reference of the CalculatorAppService project into the test project. After adding the reference, add the test project to the solution by navigating to the calculatorapp root directory, and run the command:

dotnet sln add .\CalculatorAppServices.tests\CalculatorAppServices.Tests.csproj

This command will add the test project to the solution. After successfully running the command, you should get the message “Project added to the solution”.

Writing Unit Tests

So far, we’ve created the application source project and a test project. In the application source project, there are unimplemented functions. Now let us write tests for these functions.

In the Test project, I have renamed the file UnitTest1.cs to Calculator.Test.cs and the class name to CalculatorTest. In the constructor of CalculatorTest, we need to create an object of the Calculator class. This can be done as shown in the listing below:

Calculator.Test.cs

Unit Test for Add function

In the CalculatorTest class, add a unit test method to test the add functionality as shown in the listing below:

To run the test, navigate to the CalculatorApp.Services.Tests directory and execute the command:

dotnet test

You’ll get a ‘test failed’ output because the Add function is not implemented, as shown in the image below:

image

To pass the test, implement the Add function in the Calculator class as shown in the listing below:

To run the test now, navigate to the CalculatorApp.Services.Tests director and execute the command:

dotnet test

As output, you will get the ‘test passed’ message as shown in the image

Read full articles on the Infragistics blog

Ignore Test in Unit Testing using NUnit: Part 3

We have been learning Unit Testing using NUnit together. Read first two part of this series below,

How to do Unit Test using NUnit : Part 1

Test SetUp and TearDown in Unit Testing using NUnit : Part 2

You may come across a scenario when test is itself broken. While executing test you need to ignore certain broken kind of test. Thankfully NUnit provides a very simple solution to ignore a test. By using ignore attribute you can ignore a test.

clip_image002

You can ignore a unit test as following

clip_image004

Let us go back to unit test we wrote in previous part of this series and modify test as following,


[Test]
 [Ignore("Ignore this test it is broken")]
 public void IsValidProductPrice()
 {

 p.ProductPrice = 100;
 if (p.ProductPrice > 0)
 {
 result = true;
 }

Assert.IsTrue(result, "Product Price is valid");

 }

On execution of test you will find that Test Runner has ignored this test. Test runner will skip test attributed with ignore.

clip_image002[6]

In this post we learnt how easily a test can be ignored in NUnit. I hope you find this post useful. Thanks for reading.

Test SetUp and TearDown in Unit Testing using NUnit : Part 2

Read Part 1 here: How to do Unit Test using NUnit : Part 1

In last post we talked about how to start with Unit Testing using NUnit. In this post I will discuss about following two topics,

  1. Test Setup
  2. Test Teardown

You need Test Setup and Test Teardown to remove any dependency between tests. Assume a scenario that

  • You want to create instance of a particular object before execution of any test
  • You want to delete a particular file from file system before execution of any test
  • You want to insert some test data or create some test data before execution of any test etc..

In above stated scenario you may want to create a Test Setup. Test Setup is a piece of code get executed before execution of any test.

clip_image002

Other use case could be that you want to perform a particular task after execution of each test. So once test got executed a certain task should get done and we call that Test Teardown. There could be scenario that

  • You want to destruct an instance after execution of any test
  • You want to remove test data after execution of any test
  • You want to delete a file from file system after execution of any test etc.

In above scenario you may want to create Test Teardown. Test Teardown is piece of code get executed after execution of any test.

clip_image002[6]

In NUnit you can create Test Setup and Test Teardown by using [Setup] and [TearDown] attribute on a function.

So Test Setup can be created as following,

image

And you can create Test Teardown as following

image

If there are 5 tests in your test class then these two functions will get executed 5 times. Now let us put our discussion into concrete example. Assume that you are writing Unit Test for a Product class. Product class is defined as following,

namespace MyAppToTest
{
 public class Product
 {
 double productPrice;

 public double ProductPrice
 {
 get
 {
 return productPrice;
 }

set
 {
 productPrice = value;
 }
 }
 }
}

A Unit Test is written to test valid product price as following,

[Test]
 public void IsValidProductPrice()
 {

 p.ProductPrice = 100;
 if (p.ProductPrice > 0)
 {
 result = true;
 }

Assert.IsTrue(result, "Product Price is valid");
 }

You can write Test SetUp and TearDown as following,


Product p;
 bool result;
 [SetUp]
 public void TestSetup()
 {
 p = new Product();
 result = false;

 }

[TearDown]
 public void TestTearDown()
 {
 p = null;
 result = false;
 }

The above two function will get executed each time before execution of test and after execution of test. In writing Unit Test , Test SetUp and Test TearDown are very handy and useful. I hope you find this post useful. Thanks for reading.