Enable Javascript

Please enable Javascript to view website properly

Toll Free 1800 889 7020

Looking for an Expert Development Team? Take two weeks 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:

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:

Software Development Team
Need Software Development Team?
captcha
🙌

Thank you!
We will contact soon.

Oops! Something went wrong.

Recent Blogs

Categories

NSS Note
Some of our clients
team