natemcmaster / xunit-extensions

Making XUnit.NET test projects even better
Apache License 2.0
23 stars 3 forks source link
dotnet testing xunit

Xunit Extensions

Travis build status AppVeyor build status

NuGet MyGet

This repo contains a class library of utilities and helpers for writing tests with XUnit.NET.

Getting started

Install this as a NuGet package using your favorite IDE.

Install-Package McMaster.Extensions.Xunit
dotnet add package McMaster.Extensions.Xunit

Dynamically skip tests

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

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()
    {
    }
}

Misc

Other useful helpers are included.

Task.TimeoutAfter - prevent async from running too long

[Fact]
public async Task Test()
{
    await thing.TimeoutAfter(TimeSpan.FromMinutes(4));
}