How to run NUnit tests in Visual Studio 2017?

空扰寡人 提交于 2019-11-27 06:38:47

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.

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

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.

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

You need to install 3 NuGet packages:

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

Have fun writing unit tests!

Parmeshwar Karale

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!