How to Get Started with NUnit (CSharp, Dev Containers)
In this tutorial, I'm going to show you how to get started with NUnit. To make setup easier, I'm going to use a Dev Container.

In this tutorial, I'm going to show you how to get started with NUnit. To make setup easier, I'm going to use a Dev Container.
If you don't know what a Dev Container is, see my previous article:

Step 1. Install the prerequisites
Before getting started, ensure that you have the following prerequisites installed on your Mac:
- Docker: Visit the Docker website (https://www.docker.com/) and download and install Docker Desktop for your operating system
- Visual Studio Code: Install Visual Studio Code (VS Code) on your operating system by downloading it from the official website (https://code.visualstudio.com/)
- Dev Containers: Once VS Code is installed, add the Dev Containers extension: https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers
Step 2. Create a project folder
Open up a terminal window and do the following:
mkdir -p ~/projects/csharp/nunit-container-demo
cd ~/projects/csharp/nunit-container-demo
Step 3. Start Docker
To use Dev Containers, you need Docker running.
On a Mac you can start Docker from the command line like this:
open -a Docker
You can close the Docker Desktop dialog box as long as you don't completely quit the program. But leave docker running in the background.
Step 4. Setup the dev container
To use NUnit in this tutorial, we are going to use a Dev Container setup for C# development. To create one, do the following:
- Open VS Code (note the dot (.) at the end of the command to refer to the current folder):
code .
- Open the Command Palette (Cmd+Shift+P on a Mac)
- Type in Dev Containers: Add Dev Container Configuration Files ...
- Select C# and go through the steps
- Press Reopen in Container when prompted
- Open up the VS Code Terminal window: View / Terminal on a Mac
- In the terminal window, create a .gitignore file with this command:
dotnet new gitignore
Step 5. Create a solution
In the VS Code Terminal window, do the following to create a dotnet solution:
dotnet new sln
Step 6. Create a new class library
In this step, you will create a new class library and add it to the solution.
- Create the new class library project:
dotnet new classlib -o DemoLibrary
- Change the name of the default *.cs file:
mv DemoLibrary/Class1.cs DemoLibrary/DemoLibrary.cs
Add the new DemoLibrary project to the solution:
dotnet sln add DemoLibrary/DemoLibrary.csproj
- Edit DemoLibrary/DemoLibrary.cs
- Change the code to the contents below and save the file:
namespace DemoLibrary
{
public class DemoLibrary
{
public bool IsOddNumber(int candidate)
{
throw new NotImplementedException(
"Please create a test first."
);
}
}
}
Step 7. Create the test project
In this step, you will create a new test library project and add it to the solution.
- Create the new test library project:
dotnet new nunit -o DemoLibrary.Tests
- Change the name of the default *.cs file:
mv DemoLibrary.Tests/UnitTest1.cs DemoLibrary.Tests/DemoLibrary_IsOddShould.cs
Add the new test library project to the solution:
dotnet sln add DemoLibrary.Tests/DemoLibrary.Tests.csproj
Add a reference for the class library to the test library:
dotnet add DemoLibrary.Tests/DemoLibrary.Tests.csproj reference DemoLibrary/DemoLibrary.csproj
Step 8. Add some tests
In this step, you will add some tests and run them.
- Edit DemoLibrary.Tests/DemoLibrary_IsOddShould.cs:
- Replace the contents with the code below and save it:
using NUnit.Framework;
using DemoLibrary;
namespace DemoLibrary.UnitTests.Services
{
[TestFixture]
public class DemoLibrary_IsOddhould
{
private DemoLibrary? _demoService;
[SetUp]
public void SetUp()
{
_demoService = new DemoLibrary();
}
[Test]
public void IsOdd_InputIs1_ReturnTrue()
{
var result = _demoService?.IsOddNumber(1);
Assert.IsTrue(result, "Value should be odd number");
}
}
}
- Run the tests with this command:
dotnet test
The test should fail because of the exception thrown in the class method. We will fix that in the next step.
Step 9. Implement the method
In the previous step, the test failed because a method was not implemented. Instead, it threw an error to remind you. In this step, we will update the code so that it passes.
- Edit DemoLibray/DemoLibray.cs
- Replace it with the code below and save the file:
namespace DemoLibrary
{
public class DemoLibrary
{
public bool IsOddNumber(int candidate)
{
return candidate % 2 != 0;
}
}
}
- Rerun the tests and they should pass:
dotnet test
Example project
You can find the example used in this article here:
Conclusion
In this tutorial, you learned how to:
- Create a C# project using Dev Containers
- How to setup a C# project to use NUnit
- How to create and run tests using NUnit
References
- NUnit
- Unit testing C# with NUnit and .NET Core
- NuGet in Visual Studio Code
- Install and manage NuGet packages with the dotnet CLI
- Installation | NUnit Docs
- How to use Microsoft Dev Containers (Visual Studio Code, Python)
- How to Install Dotnet on a Mac (.NET, CSharp)
- .NET Core - When to use "dotnet new sln"
- Tutorial: Create a .NET class library using Visual Studio Code