peteris.rocks

Running NUnit with MSBuild on Windows and Mono

Last updated on

How to run unit tests using NUnit.

Setup

Create a library project called Project.Tests.

Install the NUnit package via NuGet.

Create tests.

using System;

using NUnit.Framework;

namespace Tilde.Project.Tests
{
    [TestFixture]
    public class ClassTests
    {
        [Test]
        public void UnitTest()
        {
            Class obj = new Class();
            Assert.AreEqual("expected", obj.DoSomething());
        }
    }
}

Running tests

Visual Studio

Install the Nunit Test Runner extension in Visual Studio.

Open Test Explorer and click Run All.

MSBuild

Install the NUnit Runners package via NuGet.

At the following lines to your Project.Tests.csproj file at the end in <Project>:

<Target Name="RunTests" DependsOnTargets="Build">
  <PropertyGroup>
    <ExeLauncher Condition="('$(OS)' != 'Windows_NT')">mono --runtime=v4.0.30319</ExeLauncher>
  </PropertyGroup>
  <Exec Command="$(ExeLauncher) nunit-console.exe $(OutputPath)$(AssemblyName).dll"
        WorkingDirectory="$(SolutionDir)\packages\NUnit.Runners.2.6.3\tools" />
</Target>

Run your tests with

msbuild Project.Tests.csproj /t:RunTests

NUnit GUI

You can also install NUnit on your computer. It comes with a GUI. Run the GUI and load the Project.Tests.dll assembly.