Running a specific test with .NET Core and NUnit

January 23, 2017 by Anuraj

dotnet core NUnit unit test

This post is about running a specific test or specific category with .NET Core and NUnit. dotnet-test-nunit is the unit test runner for .NET Core for running unit tests with NUnit 3.

First you need to enable NUnit in dotnet core.

"dependencies": {
    "System.Runtime.Serialization.Primitives": "4.3.0",
    "NUnit": "3.4.1",
    "dotnet-test-nunit": "3.4.0-beta-2"
},
"testRunner": "nunit"

Here is the tests file.

using NUnit.Framework;

namespace Tests
{
    [TestFixture]
    public class Tests
    {
        [Test]
        public void Test1() 
        {
            Assert.IsTrue(true);
        }

        [Test]
        public void Test2() 
        {
            Assert.IsTrue(true);
        }
    }
}

And you can run dotnet test, which will run all the tests in the class.

NUnit tests running on dotnet core

If you have lot of tests, you can seperate the tests with categories, something like this.

using NUnit.Framework;

namespace Tests
{
    [TestFixture]
    public class Tests
    {
        [Test]
        [Category("Unit")]
        public void Test1() 
        {
            Assert.IsTrue(true);
        }

        [Test]
        [Category("Integration")]
        public void Test2() 
        {
            Assert.IsTrue(true);
        }
    }
}

I have a set of unit tests and another set of integration tests. And if you want to run unit tests only you can pass the parameter and execute the unit tests only.

Here is the command, dotnet test --where "cat == Unit", which will execute the tests under Unit category.

NUnit tests running on dotnet core on specific Category

If you want to run a specific test, you can do that as well using dotnet test --test Test2 command. You can find more details and command in the NUnit Console Command Line

Happy Programming :)

Copyright © 2024 Anuraj. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Powered by Jekyll. Hosted with ❤ by GitHub