Free Web Development Online Tutorials, Learn to Code
nunit testing framework c#, nunit testing .net core, c# nunit automated testing example

NUnit Testing C# Tutorial

In this NUnit Tutorial you will learn how to use NUnit Testing in C# Console application using Visual Studio 2019. NUnit is a unit-testing framework for any .Net languages.

NUnit Testing C# Example

To start using NUnit Testing Framework, either start a "NUnit Test Project" or you can install NUnit Framework from Nuget Package from your existing project. I suggest keep the test project separately, that be easy to publish clean production code.

NUnit Testing in C#

Here I have simple C# Application with a simple add function, This will help you to understand how to write NUnit test code in c# for application testing, then will write some different type of function test cases one by one.

namespace WebTrainingRoom
{
  public  class Calculator
    {
        public int Add(int a, int b)
        {
            int x = a + b;
            return x;
        }
    }
}

I have created a NUnit Test project called "NUnitTestProject", there I have test class name "CalculatorTest".

Now to test the "Add" function of "Calculator" class I have written "AddMethodTest" two different ways

using NUnit.Framework;
using WebTrainingRoom;
namespace NUnitTestProject
{
    [TestFixture]
    public class CalculatorTest
    {
        [Test]
        public void AddMethodTest()
        {
            Calculator  add = new Calculator();
            int result = add.Add(15, 65);
            Assert.That(result, Is.EqualTo(80));
        }

        [Test]
        [TestCase(15, 35, 50)]
        [TestCase(10, 45, 55)]
        [TestCase(20, 50, 60)]
        public void AddMethodTest(int num1, int num2, int expected)
        {
            Calculator add = new Calculator();
            int result = add.Add(num1, num2);
            Assert.AreEqual(expected, result);
        }
    }
}

As you can see in above example, you can define multiple test cases for any function; there I have set two correct and one incorrect test case to demonstrate how it works.

[TestCase(15, 35, 50)]
[TestCase(10, 45, 55)]
[TestCase(20, 50, 60)]

Now let's run the Test Explorer, Click on Test => Test Explorer

C# nunit testing example

After executing the Test Explorer, you will be able to all failed test cases in red color for each function. This way NUnit automated testing can help you testing functionality much faster compare to manual testing, where you test application by going through each step in GUI.

But, that does not mean automated testing can replace manual testing, automated testing can reduce the risk of function failure during manual testing, and which is faster way to test application with different test cases.

Now you know how to use NUnit Testing Framework in C#, Let’s look at more examples of different types, we may often use following attributes in our test project.

  • TestFixture

    NUnit TestFixture is the class level attribute that indicates the class contains NUnit Test Methods.

    using NUnit.Framework;
    
    [TestFixture]
    public class CalculatorTest
    {
    
    }
    

    We also can pass parameter or arguments to TestFixture class through constructor, we will see more in details with example soon.

  • Setup

    This is optional Attribute, this will indicate that the void will execute before each test. If we want to set some local variable value required for testing, then this will be the right place to set

    [TestFixture]
    public class CalculatorTest
    {
        [SetUp]        
        public void SetupCalculator()
        {
            // set some local variable value required for testing
        }
    }
    
  • Test

    This attribute indicates the method to be tested.

    [TestFixture]
    public class CalculatorTest
    {
        [Test]
        public void AddMethodTest()
        {
            Calculator add = new Calculator();
            int result = add.Add(15, 65);
            Assert.That(result, Is.EqualTo(80));
        }
    }
    
  • TestCase

    TestCase attribute used for declaring different test cases, we can set multiples test cases for each test method.

    [TestFixture]
    public class CalculatorTest
    {
            [Test]
            [TestCase(15, 35, 50)]
            [TestCase(10, 45, 55)]
            [TestCase(20, 50, 60)]
            public void AddMethodTest(int num1, int num2, int expected)
            {
                Calculator add = new Calculator();
                int result = add.Add(num1, num2);
                Assert.AreEqual(expected, result);
            }
    }
    
  • TearDown

    Indicate the method will be executed after every test case, this is optional attribute

    [TestFixture]
    public class CalculatorTest
    {
        [TearDown]
        public void CleanupValues()
        {
            // reset values
        }
    }
    
  • Ignore

    Ignores a test case

  • Category

    Categorizes the classes or test cases.

  • Repeat

    Repeat Attribute can be used on any test method to instruct test engine that the method should be executed multiple times. If any repetition fails, the remaining will not run and report failure.

    [TestFixture]
    public class CalculatorTest
    {
        [Test]
        [Repeat(10)]
        public void AddMethodTest()
        {
            Calculator add = new Calculator();
            int result = add.Add(15, 65);
            Assert.That(result, Is.EqualTo(80));
        }
    }
    
  • MaxTime

    We can set maximum execution time for any method, (this can help reducing testing time for any method that need long execution time)

    [TestFixture]
    public class CalculatorTest
    {
        [Test]
        [MaxTime(1000)]
        public void AddMethodTest()
        {
            Calculator add = new Calculator();
            int result = add.Add(15, 65);
            Assert.That(result, Is.EqualTo(80));
        }
    }
    

Comparison Constraints

There are many comparison constraints in NUnit Framework, here are few commonly used example.

  • Assert Greater Than Constraint Example
    Assert.That(result, Is.GreaterThan(10));
    Assert.That(result, Is.GreaterThanOrEqualTo(10));
    
  • Assert Less Than Constraint Example
    Assert.That(result, Is.LessThan(10));
    Assert.That(result, Is.LessThanOrEqualTo(10));
    
  • Assert Ranges Example
    Assert.That(result, Is.InRange(10, 50));
    
  • String comparison, Equal Not Equal Constraint Example
    Assert.That(result, Is.EqualTo("webtrainingroom"));
    Assert.That(result, Is.Not.EqualTo("training"));
    Assert.That(result, Is.EqualTo("CaseMatching").IgnoreCase);
    
  • Substring Constraint Example
    Assert.That(result, Does.Contain("training").IgnoreCase);
    Assert.That(result, Does.Not.Contain("training").IgnoreCase);
    
  • Regex Constraint Example
    string result = "training"; 
    Assert.That(result, Does.Match("t*g"));
    Assert.That(result, Does.Not.Match("a*x"));
    
  • Directory and File Constraints, Check if a File or Directory exists.
    Assert.That(new DirectoryInfo(path), Does.Exist);
    Assert.That(new DirectoryInfo(path), Does.Not.Exist);
    Assert.That(new FileInfo(path), Does.Exist);
    Assert.That(new FileInfo(path), Does.Not.Exist);
    
Comment
Name
Email
Website
Subscribe
 
free C# tutorial online


Attribute Routing Example in Asp.net Core MVC
Other Online Tutorial

C# .net Interview Questions Answers

.Net C# Examples | Join .Net C# Course