xUnit.net Cheat Sheet for NUnit Users

I am currently learning the xUnit.net framework as part of a new project I work on. For the last years I used NUnit for my unit and integration tests. Conceptually those two libraries aren’t that different. However, the naming of attributes and what is possible in sharing setup & clean-up code makes it worth to take a deeper look.

 

Installation

Create a class library project, right-click on References and select “Manage NuGet Packages…” to open the NuGet packages dialog. Search for xUnit and install this package:

To integrate xUnit.net into the Visual Studio Test runner you can install the package xunit.runner.visualstudio:

Check the extensive documentation and a list of all the xUnit.net NuGet packages to see how you can customize your installation.

 

Fact replaces Test

Every method annotated with Fact will be marked as a test and run by xUnit.net:

This isn’t a big change from the Test attribute you use in NUnit.

The class containing your tests must be public, but you don’t need any attributes on them (like TestFixture for NUnit).

 

Ignoring Tests

If you don’t want to execute a unit test you can skip it by providing a reason:

Again, only a small change to the Ignore attribute you know from NUnit.

 

Theories instead of TestCases

One of the reasons I use NUnit is the possibility to reuse tests by using the TestCase attribute and change only the parameters to a method. xUnit.net offers the same functionality with Theory and InlineData:

 

Asserts

The Assert class offers most of the features you know from the classic approach used by NUnit:

NUnit offers in addition the constraint model for assertions, which I find more readable. As far as I know there is no direct replacement in xUnit.net.

 

Setup & Clean-up

The biggest difference between xUnit.net and NUnit is in my opinion in the setup and clean-up code.

For every test: Constructor and Dispose

xUnit.net creates a new instance of the test class for every test it contains. This allows you to put the setup code you need in the constructor of your test class:

To clean-up after every test you use another basic functionality of .Net by implementing the IDisposable interface and putting your code in the Dispose() method:

 

Once for a test class: ClassFixture

What can be done in NUnit with a simple OneTimeSetUp needs a lot more work in xUnit.net. To have a setup and clean-up method per test class, you need to create a fixture class to place your code and implement the IClassFixture<> interface on your test class:

You only need to use your fixture class as a parameter in the constructor when you want to use it inside your test class. One benefit of this approach is that you can reuse your fixture class with other test classes – when a setup and clean-up after every class is working for you.

 

Once for multiple test classes: Collection Fixtures

Expensive setup or clean-up code may be run only once for a group of test classes. We can reuse the fixture class, create a class without code for the collection definition and then add the Collection attribute on all test classes that should use it:

 

Conclusion

xUnit.net offers more or less the same functionality I know and use in NUnit. The biggest difference is the more flexible way to reuse the same setup and clean-up code, even when this comes with an increased complexity. The traditional way of Assert.* is nearly the same and lets you quickly write tests.

So far, I don’t have a reason to switch to xUnit.net, but this may change depending on how well it works with SpecFlow on .Net Core.

1 thought on “xUnit.net Cheat Sheet for NUnit Users”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.