This one helped me:
Getting started with .NET unit testing using NUnit
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, and if you change the parameter in Is.EqualTo it will fail, etc.