Introduction

xUnit is  a newish test framework developed by the original contributor to NUnit. In essence, he seems to have distilled the best features of the various test frameworks out there – keeping the goodies and discarding unnecessary features. Rumor has it that several internal projects within Microsoft also use xUnit as their TDD platform of choice.  

While there are a lot of neat features such as easier integration into the MSBuild process (see below), the Timeout feature is what really caught my attention. This attribute lets you specify a timeout after which the test is considered a ‘failure’ – if it doesn’t return. This fits well into testing long running WCF services or controller actions. MSTest  has a similar attribute – but NUnit does not. The snippet below shows a test configured to timeout after 50 milliseconds.

   1: [Fact(Timeout=50)]

   2: public void TestThatRunsTooLong()

   3: {

   4:     System.Threading.Thread.Sleep(250);

   5: }

Facts and Theories

Tests are known as facts. See a sample test below – and notice that there’s no setup or teardown or [testfixture]. These have been done away with – in an effort to make the core test stand out without any distractions. Data driven tests (described below) are known as theories. A full list of the terminology (comparing it to NUnit terminology) is provided here.

   1: public class AgathaTests

   2:    {

   3:        [Fact]

   4:        public void DivideByZeroThrowsException()

   5:        {

   6:            Assert.Throws<System.DivideByZeroException>(

   7:                delegate

   8:                {

   9:                    DivideNumbers(5, 0);

  10:                });

  11:        }

  12:  

  13:        public int DivideNumbers(int theTop, int theBottom)

  14:        {

  15:            return theTop / theBottom;

  16:        }

Data Driven Tests (also known as theories)

The sample below shows using inline data (Math.PI and 0 ). However, xUnit can accept data from a variety of sources – including excel, sql server and c# properties.

   1: [Theory]

   2:         [InlineData(Math.PI,0)]

   3:         public void TangentExpectedResult(int angle, int expected)

   4:         {

   5:           var actualResult = Math.Tan(angle);

   6:           Assert.Equal(expected, actualResult);

   7:         }

Integration into the build process

Unlike NUnit, integrating xUnit into your build is easy as shown below.

   1: <UsingTask

   2:      AssemblyFile="..\packages\xunit.1.9.1\lib\net20\xunit.runner.msbuild.dll"

   3:        TaskName="Xunit.Runner.MSBuild.xunit" />

   4:   <Target Name="AfterBuild">

   5:    <xunit Assembly="$(TargetPath)" />

   6: </Target>

Doing away with TestFixtures, Setups and Teardowns

Another different tack that xUnit takes is doing away with Fixtures, Setups and Teardowns altogether.  In their own words ‘per-test setup and teardown creates difficult-to-follow and debug testing code, often causing unnecessary code to run before every single test is run.’

If you really want to do setups and teardowns, you can use existing c# constructs (constructors for setups, IDisposable.Dispose() for teardown)

Getting started with xUnit

Getting started with xUnit is easy – just use NuGet’s package manager (search for xUnit) to install xUnit.net and xUnit.net.Extensions (contains the data driven test framework). Also xUnit.net.Runners if you want the GUI test runner. I run all my tests with testdriven (which supports xUnit as well) – so runners are not that useful to me.

xunit_nuget

Summary

Fact – xUnit is the new testing framework in town! This post lists some of the useful features of xUnit, most of which are not available in nUnit. It is especially useful for data-driven testing, request response based testing and for integrating in an automated MSBuild process. 

Anuj holds professional certifications in Google Cloud, AWS as well as certifications in Docker and App Performance Tools such as New Relic. He specializes in Cloud Security, Data Encryption and Container Technologies.

Initial Consultation

Anuj Varma – who has written posts on Anuj Varma, Hands-On Technology Architect, Clean Air Activist.