How to run NUnit tests in Visual Studio 2017?


Translate

I've just installed VS 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 re-installed NUnit from the Nuget Package Manager to the latest version with no improvement.


All Answers
  • Translate

    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.


  • Translate

    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 Project -> Click "Manage Nuget Packages.." from context menu
    2. Go to Browse tab and search for NUnit
    3. Select NUnit3TestAdapter -> Click Install at the right side -> Click OK from Preview pop up enter image description here

  • Translate

    This one helped me: https://www.infragistics.com/community/blogs/dhananjay_kumar/archive/2015/07/27/getting-started-with-net-unit-testing-using-nunit.aspx

    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, if you change the Parameter in Is.EqualTo it will fail, etc.


  • Translate

    You need to install 3 NuGet packages:

    • Nunit
    • NUnit3TestAdapter
    • Microsoft.NET.Test.Sdk

    Have fun writing unit tests!


  • Translate
    • You have to choose the processor architecture of Unit-Tests in VS:
      Test > Test Settings > Default processor architecture

    • Test Adapter has to be open to see the tests: (VisualStudio e.g.:
      Test->Windows->Test Explorer


    Additional information whats going on you can consider at 'VS-Output-Window' and choose Drop-Down 'Show output from' and set 'Tests'


  • Translate

    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.


  • Translate

    To run or debug test in visual Studio 2017, we need to install "NUnit3TestAdapter". We can install it in any VS, but it is working properly in VS "community" version. To install this you can add through Nuget Package.