In this article, we will demonstrate getting started with xUnit.net and .NET Framework, showing you how to write and run your first set of unit tests.
Note: The examples were done with xUnit.net v2 2.4.1, .NET SDK 5.0.102, and .NET Framework 4.8. The version numbers, paths, and generated templates may differ for you, depending on which version you're using. Note that .NET Framework 4.5.2 and later is supported.
Although the .NET SDK was designed originally to support .NET Core, it can also be used to allow cross-platform .NET Framework development. We will use it to build our projects. You may also opt to use the command line tools from Visual Studio, though that is outside the scope of this document.
As of this writing, the .NET SDK is available for
download for Windows,
Linux, and macOS. Once you've downloaded and installed the SDK, open a fresh
command prompt of your choice (CMD, PowerShell, Bash, etc.) and make sure
that you can access the CLI by typing dotnet --version
. You
should be rewarded with a single line, describing the version of the
.NET Core SDK you have installed:
$ dotnet --version
5.0.102
Note: the first time you run the dotnet
command, it may
perform some post-installation steps. Once these one-time actions are done,
it will execute your command.
While you can use the .NET SDK to build .NET Framework apps on non-Windows machines, you still need a way to run them. That means Mono. If you are using a non-Windows machine, download and install Mono on your machine.
From the command line, create a folder for your test project, change into it,
and then create an empty solution file using dotnet new
:
$ mkdir MyFirstUnitTests $ cd MyFirstUnitTests $ dotnet new sln The template "Solution File" was created successfully.
Now we will create a unit test project, again with dotnet new
,
and add it to our solution:
$ dotnet new xunit The template "xUnit Test Project" was created successfully. Processing post-creation actions... Running 'dotnet restore' on ~/dev/MyFirstUnitTests/MyFirstUnitTests.csproj... Determining projects to restore... Restored ~/dev/MyFirstUnitTests/MyFirstUnitTests.csproj (in 217 ms). Restore succeeded. $ dotnet sln add . Project `MyFirstUnitTests.csproj` added to the solution.
Because the .NET SDK command line tool is optimized around supporting .NET Core,
it created a unit test project that targets the incorrect framework. Open up your
text editor and change the contents of the .csproj
file to match
the contents here:
Let's quickly review what's in this project file:
TargetFramework
specifies the target framework for your test project. You will
use the target framework name that matches the version of .NET Framework that you intend
to target (our example uses net48
, which is .NET Framework 4.8).
xunit
package brings in three child packages which include functionality
that most developers want: xunit.core
(the testing framework itself),
xunit.assert
(the library which contains the Assert
class),
and xunit.analyzers
(which enables Roslyn analyzers to detect common issues
with unit tests and xUnit.net extensibility).
xunit.runner.console
package includes a .NET Framework command line
application. This package brings a binary that you can run, rather than a library you
link against (as is the more common usage for NuGet packages). We will discuss different
ways you can run the unit tests later in this article.
Microsoft.NETFramework.ReferenceAssemblies
package includes the
reference libraries used to build your .NET Framework application. This is useful both
for Windows users (so they don't have to install the .NET Framework SDK for the specific
version you're targeting) as well as non-Windows users (which don't have the option
to install the .NET Framework SDK, since it's only available for Windows).
A single empty unit test was also generated into UnitTest1.cs
:
Let's build everything and take a look at our output folder:
$ dotnet build Microsoft (R) Build Engine version 16.8.3+39993bd9d for .NET Copyright (C) Microsoft Corporation. All rights reserved. Determining projects to restore... Restored ~/dev/MyFirstUnitTests/MyFirstUnitTests.csproj (in 93 ms). MyFirstUnitTests -> ~/dev/MyFirstUnitTests/bin/Debug/net48/MyFirstUnitTests.dll Build succeeded. 0 Warning(s) 0 Error(s) Time Elapsed 00:00:00.82 $ dir bin/Debug/net48 MyFirstUnitTests.dll MyFirstUnitTests.pdb xunit.abstractions.dll xunit.assert.dll xunit.core.dll xunit.execution.desktop.dll
We see our binary there, along with the xunit.*
dependency DLLs. What we
don't see, though, is the console runner. That's because the console runner reference
doesn't bring any libraries; it only brings binaries.
We can see those binaries by peeking into our NuGet package cache:
$ find ~/.nuget/packages/xunit.runner.console/2.4.1 -name "*.exe"
~/.nuget/packages/xunit.runner.console/2.4.1/tools/net452/xunit.console.exe
~/.nuget/packages/xunit.runner.console/2.4.1/tools/net452/xunit.console.x86.exe
~/.nuget/packages/xunit.runner.console/2.4.1/tools/net46/xunit.console.exe
~/.nuget/packages/xunit.runner.console/2.4.1/tools/net46/xunit.console.x86.exe
~/.nuget/packages/xunit.runner.console/2.4.1/tools/net461/xunit.console.exe
~/.nuget/packages/xunit.runner.console/2.4.1/tools/net461/xunit.console.x86.exe
~/.nuget/packages/xunit.runner.console/2.4.1/tools/net462/xunit.console.exe
~/.nuget/packages/xunit.runner.console/2.4.1/tools/net462/xunit.console.x86.exe
~/.nuget/packages/xunit.runner.console/2.4.1/tools/net47/xunit.console.exe
~/.nuget/packages/xunit.runner.console/2.4.1/tools/net47/xunit.console.x86.exe
~/.nuget/packages/xunit.runner.console/2.4.1/tools/net471/xunit.console.exe
~/.nuget/packages/xunit.runner.console/2.4.1/tools/net471/xunit.console.x86.exe
~/.nuget/packages/xunit.runner.console/2.4.1/tools/net472/xunit.console.exe
~/.nuget/packages/xunit.runner.console/2.4.1/tools/net472/xunit.console.x86.exe
At the time we shipped xUnit.net 2.4.1, we provided versions of xunit.console.exe
compiled against every supported version of .NET Framework that existed at the time (the only one
that's missing is .NET 4.8, since it's newer than xUnit.net 2.4.1, though of course the version
that's compiled against .NET 4.7.2 will work just fine for running 4.8 tests).
You'll also note that we ship versions that are compiled to run as 32-bit on 64-bit OSes, named
xunit.console.x86.exe
. This feature, though, only works on Windows. When you install
.NET Framework on Windows, it installs both a 32-bit and 64-bit version that live side-by-side,
and the launcher on Windows will detect whether your program is compiled for 32-bit, 64-bit,
or agnostic, and use the appropriate version of .NET Framework. When running on non-Windows machines,
you'll be running using Mono, and that only ships a 64-bit versions for Linux and macOS.
Now let's verify that everything is working. Windows user can run the console runner directly,
whereas Linux and macOS users may need to invoke the console runner using the mono
application.
$ ~/.nuget/packages/xunit.runner.console/2.4.1/tools/net472/xunit.console.exe bin/Debug/net48/MyFirstUnitTests.dll
xUnit.net Console Runner v2.4.1 (64-bit Desktop .NET 4.7.2, runtime: 4.0.30319.42000)
Discovering: MyFirstUnitTests
Discovered: MyFirstUnitTests
Starting: MyFirstUnitTests
Finished: MyFirstUnitTests
=== TEST EXECUTION SUMMARY ===
MyFirstUnitTests Total: 1, Errors: 0, Failed: 0, Skipped: 0, Time: 0.082s
$ mono ~/.nuget/packages/xunit.runner.console/2.4.1/tools/net472/xunit.console.exe bin/Debug/net48/MyFirstUnitTests.dll
xUnit.net Console Runner v2.4.1 (64-bit Desktop .NET 4.7.2, runtime: 4.0.30319.42000)
Discovering: MyFirstUnitTests
Discovered: MyFirstUnitTests
Starting: MyFirstUnitTests
Finished: MyFirstUnitTests
=== TEST EXECUTION SUMMARY ===
MyFirstUnitTests Total: 1, Errors: 0, Failed: 0, Skipped: 0, Time: 0.082s
Excellent! Everything is running.
You may have noticed that it's kind of cumbersome running tests this way from the command
line. Luckily, we can take advantage of extensibility in MSBuild to do this for us
(and, conveniently, the .csproj
file is an MSBuild project file).
If you dig around inside the ~/.nuget/packages/xunit.console/2.4.1/build
folder,
you'll notice that there is a .props
file there. Among other things, it pre-defines
some MSBuild properties that we can use when writing our own custom tasks:
First, update the PropertyGroup
to include a new conditional value. We'll define
an XunitConsole
property which will point to our chosen console runner, and when
running on non-Windows OSes, will make sure we run it using Mono:
Second, let's add a new Target
to our project file:
This is what our total .csproj
file should look like now:
Now we can use dotnet build
to run our tests:
$ dotnet build -t:Test Microsoft (R) Build Engine version 16.8.3+39993bd9d for .NET Copyright (C) Microsoft Corporation. All rights reserved. Determining projects to restore... All projects are up-to-date for restore. MyFirstUnitTests -> /home/bradwilson/dev/MyFirstUnitTests/bin/Debug/net48/MyFirstUnitTests.dll xUnit.net Console Runner v2.4.1 (64-bit Desktop .NET 4.7.2, runtime: 4.0.30319.42000) Discovering: MyFirstUnitTests Discovered: MyFirstUnitTests Starting: MyFirstUnitTests Finished: MyFirstUnitTests === TEST EXECUTION SUMMARY === MyFirstUnitTests Total: 1, Errors: 0, Failed: 0, Skipped: 0, Time: 0.063s Build succeeded. 0 Warning(s) 0 Error(s) Time Elapsed 00:00:01.49
This runs our Test
target, which will first build our project (since the
target included DependsOnTargets="Build"
).
Let's go replace that empty unit test with our first real tests.
Using your favorite text editor, open the UnitTest1.cs
file and add a
couple tests:
Now let's go run the tests again and see what happens:
$ dotnet build -t:Test Microsoft (R) Build Engine version 16.8.3+39993bd9d for .NET Copyright (C) Microsoft Corporation. All rights reserved. Determining projects to restore... All projects are up-to-date for restore. MyFirstUnitTests -> ~/dev/MyFirstUnitTests/bin/Debug/net48/MyFirstUnitTests.dll xUnit.net Console Runner v2.4.1 (64-bit Desktop .NET 4.7.2, runtime: 4.0.30319.42000) Discovering: MyFirstUnitTests Discovered: MyFirstUnitTests Starting: MyFirstUnitTests MyFirstUnitTests.UnitTest1.FailingTest [FAIL] Assert.Equal() Failure Expected: 5 Actual: 4 Stack Trace: ~/dev/MyFirstUnitTests/UnitTest1.cs(16,0): at MyFirstUnitTests.UnitTest1.FailingTest() Finished: MyFirstUnitTests === TEST EXECUTION SUMMARY === MyFirstUnitTests Total: 2, Errors: 0, Failed: 1, Skipped: 0, Time: 0.150s MyFirstUnitTests.csproj(25,5): error MSB3073: The command "xunit.console.exe MyFirstUnitTests.dll" exited with code 1. Build FAILED. MyFirstUnitTests.csproj(25,5): error MSB3073: The command "xunit.console.exe MyFirstUnitTests.dll" exited with code 1. 0 Warning(s) 1 Error(s) Time Elapsed 00:00:01.60
Now that we've gotten your first unit tests to run, let's introduce one more way to write tests: using theories.
You may have wondered why your first unit tests use an attribute named
[Fact]
rather than one with a more traditional name like Test.
xUnit.net includes support for two different major types of unit tests:
facts and theories. When describing the difference between facts and theories,
we like to say:
Facts are tests which are always true. They test invariant conditions.
Theories are tests which are only true for a particular set of data.
A good example of this is testing numeric algorithms. Let's say you want to test an algorithm which determines whether a number is odd or not. If you're writing the positive-side tests (odd numbers), then feeding even numbers into the test would cause it fail, and not because the test or algorithm is wrong.
Let's add a theory to our existing facts (including a bit of bad data, so we can see it fail):
This time when we run our tests, we see a second failure, for our theory that was given 6:
Determining projects to restore... All projects are up-to-date for restore. MyFirstUnitTests -> ~/dev/MyFirstUnitTests/bin/Debug/net48/MyFirstUnitTests.dll xUnit.net Console Runner v2.4.1 (64-bit Desktop .NET 4.7.2, runtime: 4.0.30319.42000) Discovering: MyFirstUnitTests Discovered: MyFirstUnitTests Starting: MyFirstUnitTests MyFirstUnitTests.UnitTest1.FailingTest [FAIL] Assert.Equal() Failure Expected: 5 Actual: 4 Stack Trace: ~/dev/MyFirstUnitTests/UnitTest1.cs(16,0): at MyFirstUnitTests.UnitTest1.FailingTest() MyFirstUnitTests.UnitTest1.MyFirstTheory(value: 6) [FAIL] Assert.True() Failure Expected: True Actual: False Stack Trace: ~/dev/MyFirstUnitTests/UnitTest1.cs(30,0): at MyFirstUnitTests.UnitTest1.MyFirstTheory(Int32 value) Finished: MyFirstUnitTests === TEST EXECUTION SUMMARY === MyFirstUnitTests Total: 5, Errors: 0, Failed: 2, Skipped: 0, Time: 0.171s MyFirstUnitTests.csproj(25,5): error MSB3073: The command "xunit.console.exe MyFirstUnitTests.dll" exited with code 1. Build FAILED. MyFirstUnitTests.csproj(25,5): error MSB3073: The command "xunit.console.exe MyFirstUnitTests.dll" exited with code 1. 0 Warning(s) 1 Error(s) Time Elapsed 00:00:01.14
Although we've only written 3 test methods, the test runner actually ran 5 tests; that's because each theory with its data set is a separate test. Note also that the runner tells you exactly which set of data failed, because it includes the parameter values in the name of the test.
If you're having problems discovering or running tests, you may be a victim
of a corrupted runner cache inside Visual Studio. To clear this cache, shut down all instances
of Visual Studio, then delete the folder %TEMP%/VisualStudioTestExplorerExtensions
.
Also make sure your solution is only linked against a single version of the Visual Studio runner
NuGet package (xunit.runner.visualstudio
).
If you have Visual Studio Community (or a paid-for version of Visual Studio), you can run your xUnit.net tests within Visual Studio's built-in test runner (named Test Explorer). Unfortunately, this does not include Express editions of Visual Studio (you should upgrade to the free Community Edition instead).
Edit your .csproj
file and add two package references (xunit.runner.visualstudio
and Microsoft.NET.Test.Sdk
). Your updated ItemGroup
should look like this:
Now open your solution with Visual Studio. (The screen shots and menu items here are taken from Visual Studio 2019; your version may be slightly different.)
Make sure Test Explorer is visible (go to Test > Test Explorer
). Depending
on the version of Visual Studio you have, you may need to build your test assembly before tests
are discovered. After a moment of discovery, you should see the list of discovered tests:
Click the Run All
link in the Test Explorer window, and you should see the results update
in the Test Explorer window as the tests are run:
You can click on a failed test to see the failure message, and the stack trace. You can click on the stack trace lines to take you directly to the failing line of code.