Joydip Kanjilal
Contributor

How to work with xUnit.Net framework

opinion
Feb 10, 20174 mins
Software Development

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

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 xUnit.net 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 xUnit.net runner for Visual Studio. Here’s what you would need to specify to install the xUnit.net [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 xUnit.net. Note that xUnit.net 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.

[Fact]

 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.

Joydip Kanjilal
Contributor

Joydip Kanjilal is a Microsoft Most Valuable Professional (MVP) in ASP.NET, as well as a speaker and the author of several books and articles. He received the prestigious MVP award for 2007, 2008, 2009, 2010, 2011, and 2012.

He has more than 20 years of experience in IT, with more than 16 years in Microsoft .Net and related technologies. He has been selected as MSDN Featured Developer of the Fortnight (MSDN) and as Community Credit Winner several times.

He is the author of eight books and more than 500 articles. Many of his articles have been featured at Microsoft’s Official Site on ASP.Net.

He was a speaker at the Spark IT 2010 event and at the Dr. Dobb’s Conference 2014 in Bangalore. He has also worked as a judge for the Jolt Awards at Dr. Dobb's Journal. He is a regular speaker at the SSWUG Virtual Conference, which is held twice each year.

More from this author