How to work with xUnit.Net framework

xUnit.Net is an open source unit testing tool for the .Net Framework that provides an easy way to work with data driven unit tests

xunit 1
Joydip Kanjilal

I've been using xUnit for quite some time now, and it's my Unit testing framework of choice. It's an open source unit testing tool for .Net framework that's compatible with ReSharper, CodeRush, TestDriven.Net, and Xamarin. You can take advantage of xUnit.Net to assert an exception type easily. Also, you can extend Fact or Theory attributes in xUnit.Net and it provides excellent support for writing parameterized unit tests. Here's the Github repository link for xUnit.Net.

Here's how to work with in Visual Studio. For this demonstration, we will be using Visual Studio 2015, although you can work with other compatible versions of Visual Studio as well. Now, follow these simple steps to setup your environment for working with xUnit.Net in Visual Studio.

  1. Open Visual Studio 2015 UDE
  2. Create a new project of type “Class Library”
  3. Save the project with a name
  4. Next, install xUnit.Net via the NuGet Package Manager

And that's it! To run the unit tests within Visual Studio IDE, you can use runner for Visual Studio. Here's what you would need to specify to install the [Runner: Visual Studio] package using the Package Manager Console Window:

Install-Package xunit.runner.visualstudio -Version 2.1.0

This is all you'll need to get your environment set up so that you can execute the xUnit.Net unit tests from within the Visual Studio IDE.

Facts and theories

Contrary to the popular [Test] attribute that you might be familiar with, you would need to use the [Fact] attribute to write your unit test methods using Note that supports two types of unit tests: facts and theories.

While facts are used to test invariant conditions, theories are tests that are true for a particular set of data passed as argument to the method. You would typically use the [Fact] attribute to write unit tests that have no method arguments.

However, the [Theory] attribute needs one or more DataAttribute instances to be passed as method arguments. In essence, you would want to use [Theory] attribute for writing data driven unit tests. Data driven unit tests are those that execute on varying sets of data.

Assuming that xUnit.Net and its runner for Visual Studio is installed, let's first write a simple unit test using the [Fact] attribute. Consider the following unit test method -- we will take advantage of the [Fact] attribute here.


 public void CheckEqualityTest()


     Assert.Equal(10, Sum(5, 5));


The Sum method accepts two integers and returns the sum of them.

  private int Sum(int x, int y)


    return x + y;


When you run this test, the unit test passes -- you can see that in the Test Explorer Windows in your Visual Studio IDE. Let's now explore how we can work with theories to execute unit tests that are data driven.

The following code snippet illustrates how you can work with data driven unit tests using xUnit.Net.

[Theory, InlineData("This is a data driven test", "data")]

 public void CheckInputTest(string input, string substring)


      Assert.Equal(true, input.Contains(substring));


Refer to the code snippet given above. Note the usage of the [Theory] attribute. Unless your unit tests are data driven, you should opt for the [Fact] attribute in your unit test methods. Note how parameters have been passed in the data driven unit test method named CheckInput. The InlineData attribute provides the source code data. In this example, the data is passed to the unit test method through inline values. You can have multiple InlineData attributes as well -- you just need to separate them using a comma. Here's how you can achieve this.

[Theory, InlineData("This is a data driven test", "data"),

            InlineData("This is another set of data for the data driven test", "data")]

        public void CheckInputTest(string input, string substring)


            Assert.Equal(true, input.Contains(substring));


When you execute the above data driven test, the CheckInputTest method would be executed twice -- once for each set of input data.

Copyright © 2017 IDG Communications, Inc.

How to choose a low-code development platform