Skip to content

Instantly share code, notes, and snippets.

@kainoj
Created March 24, 2018 15:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kainoj/6ab7bf98bd668bf361dac4f909d976a5 to your computer and use it in GitHub Desktop.
Save kainoj/6ab7bf98bd668bf361dac4f909d976a5 to your computer and use it in GitHub Desktop.
Dotnet: painless NUnit project setup

We'll create a simple project and set up a test. Use case: adder.

  1. Install NUnit Framework (only once per machine)
dotnet new -i NUnit3.DotNetNew.Template
  1. Create a new console project
dotnet new console -o MyProject
  1. Create a new test project in a desired directory (-n)
dotnet new nunit -n MyProjectTest
  1. Go to MyProjectTest/ and add a reference to MyProject
$ cd MyProjectTest/
$ MyTestProject/> dotnet add reference ../MyProject/MyProject.csproj
  1. Create Adder.cs in MyProject/
namespace adder {
    public class Adder {
        public int add(int a, int b) {
            return a + b;
        } 
    }
}
  1. Create AdderTest.cs in MyProjectTest/
using NUnit.Framework;
using adder;

namespace adderTest {
    public class AdderTest {
        [Test]
        public void addTest() {
            Adder adder = new Adder();
            Assert.AreEqual(5, adder.add(2, 3));
        }
    }
}
  1. Run tests
$ MyProjectTest/> dotnet test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment