This repo contains a class library of utilities and helpers for writing tests with XUnit.NET.
Install this as a NuGet package using your favorite IDE.
Install-Package McMaster.Extensions.Xunit
dotnet add package McMaster.Extensions.Xunit
In many cases, it's useful to automatically skip tests, based on conditions that cannot be determined at compile-time.
Test conditions you can use include
- SkipOnOS (skip on certain operating systems)
- SkipInEnvironment (skip if certain environment variables are set)
- SkipOnRuntimes (only run tests on Mono, .NET Core, or .NET Framework)
- SkipIfNotDocker (only run tests if running inside a Docker container)
- Your own (if you write a test that implements
ITestCondition
)
using McMaster.Extensions.Xunit;
public class MyTests
{
[SkippableFact]
[SkipOnOS(OS.Linux | OS.MacOS)
public void RunCommandPrompt()
{
Process.Start("cmd.exe", "/c dir").WaitForExit();
}
[SkippableFact]
[SkipUnlessIsNoon]
public void Test1() { }
// Implement your own conditional logic by implementing ITestCondition
public class SkipUnlessIsNoonAttribute : Attribute, ITestCondition
{
public bool IsMet => DateTime.Now.Hours == 12;
public string SkipReason { get; } = "This test can only run at noon."
}
}
Ensure tests run with a particular culture set.
using McMaster.Extensions.Xunit;
public class I18nTests
{
[Fact]
[UseCulture("fr-FR")
public void TestInFrench()
{
}
}
Other useful helpers are included.
Task.TimeoutAfter - prevent async from running too long
[Fact]
public async Task Test()
{
await thing.TimeoutAfter(TimeSpan.FromMinutes(4));
}