Using NUnit Example
Intro
An example of how to use NUnit by creating a unit test project manually.
There is a Visual Studio extension (Test Generator NUnit extension) that automates the creation of a unit test project, following some standard naming conventions.
Â
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.
Creating Unit Test Project - aka Test Runner
Â
Create a solution and application code as shown here: .NET CLI
Unit Tests should always go in a separate directory from the class directory so create a PrimeService.Tests directory
PS C:\Users\Roger\source\repos\LearningDotNet\NUnit> mkdir PrimeService.Tests
Directory: C:\Users\Roger\source\repos\LearningDotNet\NUnit
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 1/17/2021 12:30 AM PrimeService.Tests
PS C:\Users\Roger\source\repos\LearningDotNet\NUnit> dir
Directory: C:\Users\Roger\source\repos\LearningDotNet\NUnit
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 1/17/2021 12:15 AM PrimeService
d----- 1/17/2021 12:30 AM PrimeService.Tests
-a---- 1/17/2021 12:16 AM 1743 NUnit.sln
Â
Change to PrimeService.Tests directory and create the project file with the NUnit template: dotnet new nunit
This steps adds all the external dependencies and also automatically downloads them from a nuget repository via: dotnet restore
PS C:\Users\Roger\source\repos\LearningDotNet\NUnit\PrimeService.Tests> dotnet new nunit
The template "NUnit 3 Test Project" was created successfully.
Processing post-creation actions...
Running 'dotnet restore' on C:\Users\Roger\source\repos\LearningDotNet\NUnit\PrimeService.Tests\PrimeService.Tests.csproj...
Restoring packages for C:\Users\Roger\source\repos\LearningDotNet\NUnit\PrimeService.Tests\PrimeService.Tests.csproj...
Installing System.Diagnostics.TextWriterTraceListener 4.0.0.
Installing System.ComponentModel.EventBasedAsync 4.0.11.
Installing Microsoft.DotNet.PlatformAbstractions 1.0.3.
Installing Microsoft.TestPlatform.ObjectModel 15.7.2.
Installing Microsoft.Extensions.DependencyModel 1.0.3.
Installing Microsoft.TestPlatform.TestHost 15.7.2.
Installing Microsoft.CodeCoverage 1.0.3.
Installing System.Xml.XPath.XmlDocument 4.3.0.
Installing System.ComponentModel.EventBasedAsync 4.3.0.
Installing Microsoft.NET.Test.Sdk 15.7.2.
Installing NUnit3TestAdapter 3.10.0.
Installing NUnit 3.10.1.
Generating MSBuild file C:\Users\Roger\source\repos\LearningDotNet\NUnit\PrimeService.Tests\obj\PrimeService.Tests.csproj.nuget.g.props.
Generating MSBuild file C:\Users\Roger\source\repos\LearningDotNet\NUnit\PrimeService.Tests\obj\PrimeService.Tests.csproj.nuget.g.targets.
Restore completed in 4.82 sec for C:\Users\Roger\source\repos\LearningDotNet\NUnit\PrimeService.Tests\PrimeService.Tests.csproj.
Restore succeeded.
Â
Now, add theÂ
PrimeService
 class library as another dependency to the project. Use the dotnet add reference
PS C:\Users\Roger\source\repos\LearningDotNet\NUnit\PrimeService.Tests> dotnet add reference ../PrimeService/PrimeService.csproj
Reference `..\PrimeService\PrimeService.csproj` added to the project.
Â
Add the unit tests project to the solution.
Â
You can now use the solution with Visual Studio
Â
Creating A Test Case
Now that you have a solution created with a template for doing unit tests, it is time to add tests.
 In the PrimeService.Tests directory, rename the UnitTest1.cs file to PrimeService_IsPrimeShould.cs and replace its entire contents with the following code:
The [TestFixture]
 attribute denotes a class that contains unit tests. The [Test]
 attribute indicates a method is a test method.
Â
Executing Tests
To build the library and execute the test, use: dotnet test
For some reason I couldn’t figure out, the test executor decided to look for tests in the PrimeService.dll. The test does run when it executes against PrimeService.Tests. We expect the test to fail since we haven’t written any code in our library.
Â
Add Code To Library
Change IsPrime to deal with input == 1.
Specifiy the test directory to avoid running tests against the library project.
Â
Test Cases
Â
AÂ [TestCase]
 attribute is used to create a suite of tests that execute the same code but have different input arguments. You can use the [TestCase]
 attribute to specify values for those inputs.
Â
Run Tests From Studio
You can run the NUnit tests from the Test menu.
Â