Getting started with testing in .NET Core
.NET Core has been in production (RTM) since June 2016, when it was officially released at the Red Had Summit by Scott Hanselman. What a glorious moment! Announcing an open source framework at a conference for Linux! Who would have thought that this would be possible some years ago?
.NET Core 3.1 is the current (LTS) version but I'm also really looking forward to .NET 5, the one that unifies all .NET frameworks and becomes the next best version of .NET. You can download .NET 5 and take it for a spin today!
Unit testing your apps is an integral part of any CI/CD and highly recommended. Well, unit, integration, UI etc. are all equally important to ensure that your code behaves as expected before you "push it out the door"/go live! Early on, the team decided to adopt the open source xUnit framework as tool of choice for running tests on top of .NET Core.
For those developers/teams that have invested heavily on other test frameworks such as MSTest and nUnit, this presented a challenge since they would have to rewrite all the unit tests for any migrated application. Luckily, we didn’t get to this and now you can choose the tool that best meets your needs (existing codebase) or style. In this post I’ll show you how you can use the 3 big test frameworks to provide the necessary platform to write your unit tests.
Create your xUnit Test project
From all the testing frameworks, I like working with xUnit the most. And the latest templates in Visual Studio and the .NET CLI include everything you need to get off the ground with your xUnit unit tests. Open your terminal of choice and navigate to the root of your source code.
Brand new project - starting from scratch
Open the command project into your designated projects\source directory and follow the steps below:
Create the directory
mkdir testingWithXunit
cd testingWithXunit
Add the solution file
dotnet new sln testingWithXUnit.sln
Add a class project
dotnet new classlib -n myLib
Add an xUnit project
dotnet new xunit -n myLibTests
Add a reference to the class library in the xUnit project
dotnet add myLibTests/myLibTests.csproj reference myLib/myLib.csproj
Finally, add the projects to the solution
dotnet sln add myLib/myLib.csproj
dotnet sln add myLibTests/myLibTests.csproj
The above steps should create all the necessary projects, add the necessary references and set up our project to work with any editor (VS, VS Code etc). The folder structure should look like this:
The myLibTests.csproj
file should contain the right packages to allow us to run our tests either from the CLI or Visual Studio. Your csproj file should look very similar to this
Adding tests to an existing project
If you need to add tests to a project after the fact, you can follow similar steps.
Create the xUnit project
dotnet new xunit -n myLibTests
Add a reference to the class library in the xUnit project
dotnet add myLibTests/myLibTests.csproj reference myLib/myLib.csproj
Add the xunit project to the solution
dotnet sln add myLibTests/myLibTests.csproj
The important NuGet packages for xUnit to work are are:
- Microsoft.NET.Test.Sdk
- xunit
- xunit.runner.visualstudio
Once you write all your test and are ready to rock 'n roll, you can use the CLI to run your tests:
dotnet test
Testing with MSTest
MSTest is fully supported in .NET Core and it’s as easy to setup as xUnit. The VS and CLI templates support MSTest so to create a new MSTest project all you have to do is run the following commands:
Add an MStest project
dotnet new mstest -n myLibTests
Add a reference to the class library in the MSTest project
dotnet add myLibTests/myLibTests.csproj reference myLib/myLib.csproj
Finally, add the projects to the solution
dotnet sln add myLibTests/myLibTests.csproj
Testing with nUnit
Last but not least, nUnit which is also fully supported in .NET Core, both in VS and the CLI. To add a create a new nUnit project all you have to do is run the following commands:
Add an nUnit test project
dotnet new nunit -n myLibTests
Add a reference to the class library in the nUnit project
dotnet add myLibTests/myLibTests.csproj reference myLib/myLib.csproj
Finally, add the projects to the solution
dotnet sln add myLibTests/myLibTests.csproj
The new project file should look like this
As an added bonus, nUnit also outputs a TestResult.xml
file with a summary of the test run.
Using Visual Studio to run tests
Although the CLI is cool and can help run things faster, I can't deny that working inside Visual Studio is super appealing for certain scenarios and for the visual queues and great feedback it offers. In terms of testing, the Visual Studio team has reimplemented the Test Explorer which provides a much improved way for running and debugging tests.
Also, if you're running the Enterprise Version of VS2019, then you can also boost your productivity with Live Unit Testing
So make sure to leverage the tools in your disposal and use whatever makes you more productive!
Summary
There you have it - 3 different .NET Core test frameworks that allow you to test your code in the same, consistent and performant way. I've added a small sample .NET Core project with 3 different tests on GitHub if you want to clone and test it out.
Feel free to leave a comment if you have any questions or suggestions.