How to Create and Run Unit Tests in C# .NET – A Quick Tutorial


This tutorial will show you how to create and run unit tests under Visual Studio. We use C# but the same principle can be applied to other programming languages e.g. C++. Let’s first create a class library (C# project):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Example: How to Create Unit Tests
// helloacm.com
 
using System;
 
namespace ClassLibrary1
{
    public class Class1
    {
        public int GetEven()
        {
            return new Random().Next()*2;
        }
    }
}
// Example: How to Create Unit Tests
// helloacm.com

using System;

namespace ClassLibrary1
{
    public class Class1
    {
        public int GetEven()
        {
            return new Random().Next()*2;
        }
    }
}

We have a method GetEven() that intends to return a random even number. Now, we can create Test Project in the [New Project] Wizard.

visual-studio-create-test-project How to Create and Run Unit Tests in C# .NET - A Quick Tutorial c # unit tests

visual-studio-create-test-project

And, let’s include them both in the same solution space:

visual-studio-create-test-project-solution How to Create and Run Unit Tests in C# .NET - A Quick Tutorial c # unit tests

visual-studio-create-test-project-solution

And, if we complete the [UnitTest1.cs], to fulfil the TestMethod1.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using ClassLibrary1;
using Microsoft.VisualStudio.TestTools.UnitTesting;
// Example: How to Create Unit Tests
// helloacm.com
 
namespace UnitTestProject1
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            Assert.IsTrue(new Class1().GetEven() % 2 == 0);
        }
    }
}
using ClassLibrary1;
using Microsoft.VisualStudio.TestTools.UnitTesting;
// Example: How to Create Unit Tests
// helloacm.com

namespace UnitTestProject1
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            Assert.IsTrue(new Class1().GetEven() % 2 == 0);
        }
    }
}

We need to Add Reference [ClassLibrary1] in the Test Solution so that we can reference projects using ClassLibrary1. Then in the Test Method, we can Assert that the GetEven method always returns an even number. The developer should write this because they know better at the code they are developing.

If we go to “Test” — “Run” — “All Tests” or (Ctrl + R, A) in Visual Studio, it will then look for all test classes and run them automatically.

visual-studio-test-explorer-pass How to Create and Run Unit Tests in C# .NET - A Quick Tutorial c # unit tests

visual-studio-test-explorer-pass

And, for example, if we change the Assert to (odd number)

Assert.IsTrue(new Class1().GetEven() % 2 == 1);

and re-run the tests, we will get this:

visual-studio-test-explorer-fail How to Create and Run Unit Tests in C# .NET - A Quick Tutorial c # unit tests

visual-studio-test-explorer-fail

The Resharper has an enhanced GUI for Unit Tests.

visual-studio-resharper-run-test How to Create and Run Unit Tests in C# .NET - A Quick Tutorial c # unit tests

visual-studio-resharper-run-test

It is also possible to run all tests at command line using mstest.exe from Microsoft:

mstest.exe /testcontainer:UnitTestProject1.dll /resultsfile:res.trx

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
541 words
Last Post: C# LINQ: Possible Multiple Enumeration of IEnumerable (Resharper)
Next Post: How to Show Tweets Archive in WordPress using PHP and MySQL with Crontab Support?

The Permanent URL is: How to Create and Run Unit Tests in C# .NET – A Quick Tutorial

One Response

  1. Nik

Leave a Reply