Lesson 05 of 93 · 6 min read · Updated
xUnit v2 vs v3: ValueTask Lifetimes, TestContext.Current and Runtime Skips
On this page
xUnit v3 looks almost the same as v2 when you read a test: [Fact], [Theory] and Assert.Equal are all still there. The real changes are in the architecture and in how tests run. This lesson walks through a v3 test class and shows each change next to its v2 equivalent: the new IAsyncLifetime signatures, TestContext.Current, the test cancellation token and runtime skipping with Assert.SkipWhen.
Technical Requirements
- .NET 11 SDK (projects target
net11.0) xunit.v34.0.3xunit.runner.visualstudio4.0.1 andMicrosoft.NET.Test.Sdk18.12.0 (to run underdotnet testwith VSTest)coverlet.collector10.1.1 (optional)
What Changes, and What Does Not
The way you write a test stays the same. Arrange-Act-Assert does not care which major version of the framework you use. What changes is this:
- A v3 test project is an executable (
OutputTypeisExe). You can start it directly withdotnet run. - Microsoft.Testing.Platform (MTP) support is built in. The project can still run through the VSTest adapter.
IAsyncLifetimereturnsValueTaskinstead ofTask, and it now derives fromIAsyncDisposable.TestContext.Currentgives you the running test, its cancellation token and its output from anywhere.Assert.SkipandAssert.SkipWhenlet a test decide at run time that it should be skipped.
The Project File
M01.Foundations.XUnitV3Examples/M01.Foundations.XUnitV3Examples.csproj
The package is xunit.v3, not xunit. The two MTP properties are set to false so the project runs under the classic VSTest mode like the rest of the solution. The next lesson explains these properties and the three ways to run this project.
Async Lifetime with ValueTask
M01.Foundations.XUnitV3Examples/XUnitV3FeatureTests.cs
IAsyncLifetime is how a test class does asynchronous setup and cleanup, for example starting a container or seeding a database. In v3 both methods return ValueTask. Because the interface now derives from IAsyncDisposable, DisposeAsync is the standard IAsyncDisposable.DisposeAsync. When you migrate, every class that implements IAsyncLifetime fails to compile until you change Task to ValueTask. That is a good thing: the compiler gives you the full list of places to fix.
TestContext.Current and the Cancellation Token
M01.Foundations.XUnitV3Examples/XUnitV3FeatureTests.cs
In v2, the only way to write to the test output was to take an ITestOutputHelper in the constructor and keep it in a field. In v3, TestContext.Current is available from anywhere during a test, including helper methods and fixtures. It also tells you which test is running, through TestMethod. The string passed to WriteLine means "This line is written to the test output."
The cancellation token matters more than it looks. When a test run is cancelled, or a test exceeds its timeout, xUnit v3 signals this token. If you pass it to HttpClient, EF Core or Task.Delay, a stuck test stops quickly instead of blocking the whole run. Make it a habit to pass TestContext.Current.CancellationToken to every async call in a test.
Skipping at Run Time
M01.Foundations.XUnitV3Examples/XUnitV3FeatureTests.cs
[Fact(Skip = "...")] in v2 always skips. It cannot react to the machine the test runs on. Assert.SkipWhen(condition, reason) checks the condition while the test runs and reports the test as skipped, not as passed or failed. Typical uses are tests that need Docker, a specific operating system or a secret that exists only in CI. The reason string here means "Skipped because M01_SKIP_DEMO=1 is set." Set that environment variable to 1 and run the tests again to see the skip.
The [Theory] at the end is unchanged from v2. This is the point of the sample: most of your test code moves to v3 without edits.
Common Mistake: Mixing v2 and v3 in One Project
The xunit (v2) and xunit.v3 packages cannot live in the same project. In this solution, shared build settings add xUnit v2 automatically to every project whose name ends with .Tests. That is why this project is named M01.Foundations.XUnitV3Examples and deliberately does not end with .Tests. If you add v3 to a project that already gets v2 from a shared props file, you will get conflicting types such as two different FactAttribute classes.
When you migrate, check four things: the packages (xunit becomes xunit.v3), <OutputType>Exe</OutputType>, the target framework (net8.0 or later, or net472 or later) and the IAsyncLifetime signatures.
Summary
- Writing tests in xUnit v3 is almost identical to v2; the changes are in architecture and execution.
- A v3 test project is an executable and has built-in Microsoft.Testing.Platform support.
IAsyncLifetimenow returnsValueTaskand derives fromIAsyncDisposable.TestContext.Currentexposes the running test, its output and a cancellation token you should pass to async calls.Assert.SkipWhenskips a test at run time; never referencexunitandxunit.v3from the same project.