Enable Javascript

Please enable Javascript to view website properly

Toll Free 1800 889 7020

Looking for an Expert Development Team? Take 2 weeks Free Trial! Try Now

Automated Unit Tests with NUnit in .NET Core App

Automated Unit Tests:

This is a way of testing the application code written by a developer to develop any application. Unit tests are an integral part of Software application development life cycle. They might be some extra time consuming, but they do provide some special benefits when you have to scale your application as per the client needs.

NUnit:

It is a framework library that simplifies the process of writing unit test to test application code. It is the most popular framework out in market and is the oldest one with a lot of features. It is an open source project and now a part of .net foundation.

NUNIT

You can browse their official website for documentation purpose and most of their document is publicly available on github repo.

NUNIT
NUNIT

Agenda:

In this article, we will developed a .net core application with nunit template provided as a part of .net core by Microsoft. We will install the required libraries or update the existing ones that are provided as a part of template. Later we will learn how to write unit tests and run them in the Test Explorer window of Visual Studio.

Creating the application:

Step 1: Start Visual studio and create a new project.

NUNIT

Step 2: From the project templates, select NUnit Test project template for .net core as shown below.

NUNIT

Step 3: Provide a meaningful name to the project and hit create button.

NUNIT
NUNIT

Step 4: Once the project is created, go the nuget packages library by right clicking on project name and select manage nuget packages option. Make sure the below mentioned packages are installed with the latest version. If not, install them or upgrade as per availability.

NUNIT

Step 5: Now we are done with project creation and packages installation. Let’s start writing the Unit Test cases.

Go to UnitTest1.cs file and replace the code with the code below.

using NUnit.Framework; namespace NUnitDotNetCoreDemo { [TestFixture] public class Tests { [SetUp] public void Setup() {} [Test] public void Test1() { Assert.Pass(); Assert.Fail(); } [Test] public void IsNull() { object obj = null; Assert.That(obj, Is.Null); } [Test] public void IsNotNull() { Assert.That(42, Is.Not.Null); } [Test] public void IsTrue() { Assert.That(2 + 2 == 4, Is.True); } [Test] [Ignore("I want to ignore for testing purpose.")] public void IsFalse() { Assert.That(2 + 2 == 5, Is.False); } [Test] public void IsNaN() { double d = double.NaN; Assert.That(d, Is.NaN); } [Test] public void CollectionContainsTests() { int[] iarray = new int[] { 1, 2, 3 }; Assert.That(iarray, Has.Member(3)); Assert.That(iarray, Has.All.LessThan(10)); } [Test] public void PositiveWarning() { Warn.If(true, "This will emit a warning"); Warn.Unless(false, "This will emit a warning"); Assert.Pass("This test passes despite the warnings."); } } }

Explanation:

TestFixture: This attribute makes a class testable and tells the test runner to consider this class as a Unit Testing class.

Setup method: NUnit has a setup method that is called each time a unit test is executed. So, it works like a single place where you can define that objects that are required in each unit test.

Unit tests should not return anything so each unit test should be of void return type.

Each unit test method should be decorated with Test attribute which tells the test runner that during unit test execution it has to process that unit test method. If a method is not decorated with Test attribute, then that method will be considered as a test method.

Unit Test works on the principle of AAA (Arrange, Act, Assert)

A. Arrange: This part should arrange the object that are required to create the unit test. This includes object creation, or mock data creation.

B. Act: This part includes the actual calling of business logic that you want to test. This part returns a result which is needed for verification of correct data.

C. Assert: This is the final part and at this part NUnit plays an important role. Other frameworks like MS Test, they do provide the same feature to verify that the result is correct or not, but its hard to remember.

If you look the code sample that we have used, it contains Assert Statements which are easily understood as they feel like plain English. So, developer does not have to remember any syntaxes to write unit tests which eventually increases developer’s productivity.

Ignore Attribute: For example, you have a large application having a lot of features. Now, you client wants you to remove one of the features out of all. So, in ideal case you will either delete the code of the particular feature or comment it out for future needs. And the same you have to do with corresponding unit tests which is a bad practice. Instead, we can decorate the class or method with Ignore attribute and while running unit test cases, test compiler will ignore those tests.

To run the unit tests, open Test explorer window and click on green icon on the top as shown below.

NUNIT
NUNIT

Below is the final output, when you run all unit tests in one go.

NUNIT
Software Development Team
Need Software Development Team?
captcha
🙌

Thank you!
We will contact soon.

Oops! Something went wrong.

Recent Blogs

Categories

NSS Note
Trusted by Global Clients