How to run NUnit tests in Visual Studio 2017?

2019-01-10 16:29发布

问题:

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.

回答1:

Add the NUnit test adapter NuGet package to your test projects

  • 2.* (https://www.nuget.org/packages/NUnitTestAdapter/)
  • 3.* (https://www.nuget.org/packages/NUnit3TestAdapter/)

Or install the Test Adapter visual studio extension. There is one for

  • 2.* (https://marketplace.visualstudio.com/items?itemName=NUnitDevelopers.NUnitTestAdapter)
  • 3.* (https://marketplace.visualstudio.com/items?itemName=NUnitDevelopers.NUnit3TestAdapter).

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.



回答2:

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


回答3:

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.



回答4:

  • 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'



回答5:

You need to install 3 NuGet packages:

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

Have fun writing unit tests!



回答6:

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.