The XUnit team decided to do a major overhaul of the XUnit libraries and created completely new V3 packages. So don't expect backwards compatibility but a significant architectural shift that brings improved performance, better isolation, and modernized APIs to .NET testing. While the migration requires some work, the benefits should make it worthwhile for most projects.
In this post I’ll share some of the migration problems I encountered and how to fix them.
XUnit.Abstractions is not recognized
This is an easy one. The XUnit.Abstractions has become an internal namespace and should no longer be referenced. Just remove any
using Xunit.Abstractions
statement from you code.
No v3 version of Serilog.Sinks.XUnit
After switching to the v3 version of the Xunit packages, I noticed that the old XUnit v2 version was still used somewhere causing the following compiler error:
The type 'FactAttribute' exists in both 'xunit.core, Version=2.4.2.0, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c' and 'xunit.v3.core, Version=3.0.0.0, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c'
The root cause turned out to be the Serilog.Sinks.XUnit NuGet package which doesn’t support xUnit.v3 yet at the moment of writing this post.
I fixed it by creating my own fork and compiling a new version of this NuGet package using xUnit.v3.
Bye, bye [UseCultureAttribute]
Before xUnit v3 I had to create my own [UseCultureAttribute]
to use a different culture than the default culture to run my tests. Starting from v3, we can override the culture using either a command line switch or through our xunit.runner.json
configuration file:
IAsyncLifetime changes
IAsyncLifetime
now inherits from IAsyncDisposable
, and the disposal behavior has changed:
Remark: Also notice the use of ValueTask instead of Task
Cancellation token warning
This last one isn’t really a show stopper (unless you have Treat warnings as errors active like me) but still useful to apply.
xUnit1051: Calls to methods which accept CancellationToken should use TestContext.Current.CancellationToken to allow test cancellation to be more responsive.
Remark: I noticed that you also have the option to inject an ITestContextAccessor instance as described here: https://anthonysimmon.com/xunit-cancellationtoken-support/
More about this new TestContext class can be found here: What's New in v3?