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:
This one helped me:
Getting started with .NET unit testing using NUnit
Basically:
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.
You need to install three NuGet packages:
NUnit
NUnit3TestAdapter
Microsoft.NET.Test.Sdk
You have to choose the processor architecture of unit tests in Visual Studio: menu Test → Test Settings → Default processor architecture
Test Adapter has to be open to see the tests: (Visual Studio e.g.: menu Test → Windows → Test 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'.
For anyone having issues with Visual Studio 2019:
I had to first open menu Test → Windows → Test Explorer, and run the tests from there, before the option to Run / Debug tests would show up on the right click menu.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With