175

I've just installed Visual Studio 2017. I have a project using NUnit for the test cases. Ctrl + R - T no longer runs the tests, and the Test Explorer no longer finds any test cases marked with the TestCase attribute.

Is there a way to get NUnit running yet, or an update I could find?

I reinstalled NUnit from the NuGet Package Manager to the latest version with no improvement.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131

9 Answers9

216

Add the NUnit test adapter NuGet package to your test projects

Or install the Test Adapter Visual Studio extension. There is one for

I prefer the NuGet package, because it will be in sync with the NUnit version used by your project and will thus automatically match the version used in any build server.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jessehouwing
  • 106,458
  • 22
  • 256
  • 341
  • 3
    Perfecto!! Worked on mine with VS 2017. If installed Test Adapter 2.0 then should match -> update Nuget package NUnit 2.x.x version. If Test Adapter 3.0 -> update nuget package to 3.x.x. Thank you @jessehouwing – Anand Gangadhara Jun 03 '17 at 13:34
  • 21
    Also make sure you have referenced [Microsoft.NET.Test.Sdk](https://www.nuget.org/packages/Microsoft.NET.Test.Sdk/), at least if you are targeting .NET Core. I overlooked that I was missing that reference. – Dan Jagnow Sep 25 '17 at 20:32
  • 5
    I was stuck with this for quite some time, as none of the solutions worked. Then I figured out from [this](http://hermit.no/net-core-setup/) line that a class library with target ".NET Standard" does not work. The test project has to target .NET Core. Also, `Microsoft.NET.Test.Sdk` NuGet is required. – Arghya C Jan 04 '18 at 17:28
  • 3
    Worth mentioning that another good reason for choosing NUnit test adapter NuGet package is that your colleagues won't have to install any VS extensions. And your build server also don't need any extra installments. – Jim Aho Mar 14 '18 at 09:15
  • 1
    i installed both but its not obvious how to run these tests. looked at View / other windows but can't find the nunit test explorer – Sonic Soul May 31 '18 at 20:15
  • #sonicsoul, the tests appear in the standard Visual Studio test explorer window. – jessehouwing Jun 01 '18 at 06:26
  • This might depend on the machine or version, but I had to restart Visual Studio after installing the NUnit and TestAdapter NuGet packages. Only then did 'Run tests' appear in the context menus for projects. Obvious perhaps, but sadly not to me so I post this in case it helps others! – Neil T Mar 18 '21 at 09:14
42

You need to install NUnitTestAdapter. The latest version of NUnit is 3.x.y (3.6.1) and you should install NUnit3TestAdapter along with NUnit 3.x.y

To install NUnit3TestAdapter in Visual Studio 2017, follow the steps below:

  1. Right click on menu Project → click "Manage NuGet Packages..." from the context menu
  2. Go to the Browse tab and search for NUnit
  3. Select NUnit3TestAdapter → click Install at the right side → click OK from the Preview pop up

Enter image description here

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
26

This one helped me:

Getting started with .NET unit testing using NUnit

Basically:

  • Add the NUnit 3 library in NuGet.
  • Create the class you want to test.
  • Create a separate testing class. This should have [TestFixture] above it.
  • Create a function in the testing class. This should have [Test] above it.
  • Then go into TEST/WINDOW/TEST EXPLORER (across the top).
  • Click run to the left-hand side. It will tell you what has passed and what has failed.

My example code is here:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;

namespace NUnitTesting
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }

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

    [TestFixture]
    public class TestLogging
    {
        [Test]
        public void Add()
        {
            Maths add = new Maths();
            int expectedResult = add.Add(1, 2);
            Assert.That(expectedResult, Is.EqualTo(3));
        }
    }
}

This will return true, and if you change the parameter in Is.EqualTo it will fail, etc.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Lee
  • 447
  • 6
  • 9
19

You need to install three NuGet packages:

  • NUnit
  • NUnit3TestAdapter
  • Microsoft.NET.Test.Sdk
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
David Liang
  • 20,385
  • 6
  • 44
  • 70
  • 2
    This is the only solution that reliably works if you're trying to run tests in a project you didn't create. The VS extension will silently fail. – Josh Noe Mar 16 '21 at 19:10
10
  • You have to choose the processor architecture of unit tests in Visual Studio: menu TestTest SettingsDefault processor architecture

  • Test Adapter has to be open to see the tests: (Visual Studio e.g.: menu TestWindowsTest Explorer


Additional information what's going on, you can consider at the Visual Studio 'Output-Window' and choose the dropdown 'Show output from' and set 'Tests'.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
MichiBack
  • 1,310
  • 13
  • 12
2

For anyone having issues with Visual Studio 2019:

I had to first open TestWindowsTest Explorer, and run the tests from there, before the option to Run / Debug tests would show up on the right click menu.

Garrison Becker
  • 481
  • 3
  • 12
1

Using the CLI, to create a functioning NUnit project is really easy. The template does everything for you.

dotnet new -i NUnit3.DotNetNew.Template
dotnet new nunit

On .NET Core, this is definitely my preferred way to go.

Dejan
  • 9,150
  • 8
  • 69
  • 117
1

Install the NUnit and NunitTestAdapter package to your test projects from Manage Nunit packages. to perform the same: 1 Right-click on menu Project → click "Manage NuGet Packages". 2 Go to the "Browse" tab -> Search for the Nunit (or any other package which you want to install) 3 Click on the Package -> A side screen will open "Select the project and click on the install.

Perform your tasks (Add code) If your project is a Console application then a play/run button is displayed on the top click on that any your application will run and If your application is a class library Go to the Test Explorer and click on "Run All" option.

NIRAJ RANA
  • 11
  • 2
0

To run or debug tests in Visual Studio 2017, we need to install "NUnit3TestAdapter". We can install it in any version of Visual Studio, but it is working properly in the Visual Studio "community" version.

To install this, you can add it through the NuGet package.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131