Getting Started with (desktop)

In this article, we will demonstrate getting started with, showing you how to write and run your first set of unit tests.

Note: The examples were done with v2.1. The version numbers and paths may differ for you, depending on which version you're using.

Create a class library project

Let's start by creating a class library project, targeting .NET 4.5 (or later). Open Visual Studio, and choose File > New > Project:

Add a reference to

In Solution Explorer, right click the new project, and choose Manage NuGet Packages:

Now in the NuGet package manager window, take the following steps:

  1. On the left side, ensure is selected under Online
  2. In the search box in the upper right corner, type xunit
  3. Select the package, and click Install

This package (xunit) is what's called a meta-package; that is, it's a package that exists just so you can get references to several other packages. In particular, it brings packages that include the core unit testing framework and the assertion framework. If you open packages.config, you'll see all the packages that get referenced:

<?xml version="1.0" encoding="utf-8"?>
  <package id="xunit" version="2.1.0" targetFramework="net45" />
  <package id="xunit.abstractions" version="2.0.0" targetFramework="net45" />
  <package id="xunit.assert" version="2.1.0" targetFramework="net45" />
  <package id="xunit.core" version="2.1.0" targetFramework="net45" />
  <package id="xunit.extensibility.core" version="2.1.0" targetFramework="net45" />
  <package id="xunit.extensibility.execution" version="2.1.0" targetFramework="net45" />

Write your first tests

When you created the project, Visual Studio automatically created a file named Class1.cs and opened it for you. Inside this class, add a couple tests:

using Xunit;

namespace MyFirstUnitTests
    public class Class1
        public void PassingTest()
            Assert.Equal(4, Add(2, 2));

        public void FailingTest()
            Assert.Equal(5, Add(2, 2));

        int Add(int x, int y)
            return x + y;

Build the solution to ensure that the code compiles. Now that you've written the first test, we need a way to run it. Let's install the NuGet package with the console runner.

Add a reference to console runner

Once again, right click on the project in Solution Explorer and choose Manage NuGet Packages. This time, you're going to search for (and install) a package named xunit.runner.console:

Unlike the previous package (which added references to the unit testing framework), this package is what's known as a solution-level package. Instead of having assemblies to reference, it adds some tools in your solution folder. We will use one of these tools—the console runner—to run your unit tests.

Run tests with the console runner

Open a command prompt or PowerShell command window. In the window, navigate to the root folder of your solution.

To run the console runner, use a command like the one hightlighted below. You should see output similar to this:

> packages\xunit.runner.console.2.1.0\tools\xunit.console MyFirstUnitTests\bin\Debug\MyFirstUnitTests.dll console test runner (64-bit .NET 4.0.30319.34014)
Copyright (C) 2016 .NET Foundation.

Starting: MyFirstUnitTests.dll
   MyFirstUnitTests.Class1.FailingTest [FAIL]
      Assert.Equal() Failure
      Expected: 5
      Actual:   4
      Stack Trace:
         MyFirstUnitTests\Class1.cs(16,0): at MyFirstUnitTests.Class1.FailingTest()
Finished: MyFirstUnitTests.dll

   MyFirstUnitTests.DLL  Total: 2, Failed: 1, Skipped: 0, Time: 0.165s, Errors: 0

Note: your path names my vary, depending on what name you chose for your project and which version of you installed.

The console runner has several command line options, which include options for parallelization, test filtering, and result reporting. To learn more about the console runner options, run the console runner with no command line options.

Now that we've gotten your first unit tests to run, let's introduce one more way to write tests: using theories.

Write your first theory

You may have wondered why your first unit tests use an attribute named [Fact] rather than one with a more traditional name like Test. 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 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):

public void MyFirstTheory(int value)

bool IsOdd(int value)
    return value % 2 == 1;

This time when we compile and run our tests, we see a second failure, for our theory that was given 6: console test runner (64-bit .NET 4.0.30319.34014)
Copyright (C) 2016 .NET Foundation.

Starting:  MyFirstUnitTests.dll
   MyFirstUnitTests.Class1.FailingTest [FAIL]
      Assert.Equal() Failure
      Expected: 5
      Actual:   4
      Stack Trace:
         MyFirstUnitTests\Class1.cs(16,0): at MyFirstUnitTests.Class1.FailingTest()
   MyFirstUnitTests.Class1.MyFirstTheory(value: 6) [FAIL]
      Assert.True() Failure
      Stack Trace:
         MyFirstUnitTests\Class1.cs(30,0): at MyFirstUnitTests.Class1.MyFirstTheory(Int32 value)
Finished: MyFirstUnitTests.dll

   MyFirstUnitTests.DLL  Total: 5, Failed: 2, Skipped: 0, Time: 0.176s, Errors: 0

Although we've only written 3 test methods, the console 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.

Running tests with Visual Studio

Important note: If you've previously installed the Visual Studio Runner VSIX (Extension), you must uninstall it first. The Visual Studio runner is only distributed via NuGet now. To remove it, to go Tools > Extensions and Updates. Scroll to the bottom of the list, and if is installed, uninstall it. This will force you to restart Visual Studio.

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 project 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 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).

Right click on the project in Solution Explorer and choose Manage NuGet Packages. Search for (and install) a package named xunit.runner.visualstudio:

Make sure Test Explorer is visible (go to Test > Windows > Test Explorer). Every time you build your project, the runner will discover unit tests in your project. 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.

If you have Visual Studio Ultimate and you have the CodeLens feature turned on, your source code will now show icons and text related to your tests:

CodeLens not only shows you the last run status of your unit tests, it also offers a convenient place to click to run an individual test.

Note: Only v2 supports pre-enumeration of theories; when discovering theories with v1, it will only show a single test method for the theory.

Note: CodeLens does not support icons for pre-enumerated theories; this is a limitation of the Visual Studio test runner. In a future release, we will enable the ability to turn off theory pre-enumeration of theory data for users who would prefer to have the CodeLens icons rather than individually runnable tests.

Console runner return codes

Starting with version 2.2, the following return codes are used by the console runner:

Return code Meaning
0 The tests ran successfully.
1 One or more of the tests failed.
2 The help page was shown, either because it was requested, or because the user did not provide any command line arguments.
3 There was a problem with one of the command line options passed to the runner.
4 There was a problem loading one or more of the test assemblies (for example, if a 64-bit only assembly is run with the 32-bit test runner).
Copyright © 2017 .NET Foundation. Contributions welcomed at