/Xunit.StaFact

Run xunit tests on STA thread with various SynchronizationContexts to emulate windowing applications for more reliable unit testing.

Primary LanguageC#OtherNOASSERTION

Xunit.StaFact

Build status NuGet package

This project allows for Xunit tests to run on an STA thread instead of the default MTA. It also offers attributes that apply the WPF or Windows Forms SynchronizationContexts to that thread to more fully emulate a windowed application and to ensure that async tests resume execution on the same thread as they would in a real app.

A pure portable UIFactAttribute is also offered for basic UI thread behavior that doesn't tie directly to a specific GUI framework.

Installation

This project is available as a NuGet package

Samples

[WpfFact]
public async Task WpfFact_OnSTAThread()
{
    Assert.Equal(ApartmentState.STA, Thread.CurrentThread.GetApartmentState());
    await Task.Yield();
    Assert.Equal(ApartmentState.STA, Thread.CurrentThread.GetApartmentState()); // still there
}

See more samples.