How to run NUnit tests in Visual Studio 2017?

2019-01-10 16:16发布

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.

6条回答
甜甜的少女心
2楼-- · 2019-01-10 16:17

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
查看更多
Animai°情兽
3楼-- · 2019-01-10 16:21

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.

查看更多
可以哭但决不认输i
4楼-- · 2019-01-10 16:33

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.

查看更多
Rolldiameter
5楼-- · 2019-01-10 16:38

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.

查看更多
家丑人穷心不美
6楼-- · 2019-01-10 16:39

You need to install 3 NuGet packages:

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

Have fun writing unit tests!

查看更多
地球回转人心会变
7楼-- · 2019-01-10 16:40
  • 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'

查看更多
登录 后发表回答