NUnit - Unit Testing Framework For C#
Intro
My notes on using this open source tool for unit testing C# work
Documentation
Introduction To NUnit Testing Framework - Has a good step-by-step example using Visual Studio.
Unit testing C# with NUnit and .NET Core - A tutorial on unit testing - Example using dotnet CLI.
Using NUnit Example - My notes on following the above example.
Installation
If you create a unit test project with the Test Generator NUnit extension or dotnet new nunit, then the needed references and packages will be downloaded and set up for you. See example: Using NUnit Example
Another way to install is via NuGet packages. After creating a project, you can add the needed NuGet dependencies (for more info on using Visual Studio’s NuGet Package Manager see here):
These are the 3 packages you need to add.
Tips And Tidbits
There is a Visual Studio extension (Test Generator NUnit extension) that automates the creation of a unit test project, following some standard naming conventions.
The
[TestFixture]
attribute denotes a class that contains unit tests.There should only be one TestFixture per file and it should correspond to one class (per file) in your application.
The
[Setup]
attribute indicates a method that will be run BEFORE each test.A
[TearDown]
attribute indicates a method to be run AFTER each test is executed.The
[Test]
attribute indicates a method is a test method.A
[TestCase]
attribute is used to create a suite of tests that execute the same code but have different input arguments.You can pass parameters like so:
[TestCase(parameter)]
Cheat Sheets
From Automate the Planet: