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
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."
}
}
## Test in different cultures
Ensure tests run with a particular culture set.
```c#
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));
}