Combining RequiresSTA and Timeout attributes on a test fails
- by Peter Lillevold
I have a test that opens and closes a WPF Window and thus requires the STA threading apartment. To safeguard the test against the window staying open (and thus hang the test indefinitely) I wanted to use the Timeout attribute.
The problem is that applying the Timeout attribute causes the test to fail on timeout regardless of whether the test works or not. Without the attribute everything works fine.
My theory is that Timeout causes the test to be executed on a new thread that does not inherit the STA apartment.
Is there another way to have both STA and the timeout safeguard in NUnit?
My test looks something like this:
[Test, RequiresSTA, Timeout(300)]
public void Construct()
{
var window = new WindowView();
window.Loaded += (sender, args) => window.Close();
var app = new Application();
app.Run(window);
try
{
// ...run system under test
}
finally
{
app.Shutdown();
}
}