I use console runner (xUnit 2.4.1) to execute set of tests. Some tests should fail in case they need too long to finish, but it seems like Timeout
attribute is ignored.
This is my simple test method that should fail:
[Fact(Timeout = 1000)]
public void ShouldTimeout()
{
Thread.Sleep(15000);
}
I execute runner with the option -parallel none
in order to avoid undefined behaviour:
xunit.console.exe tests.dll -parallel none -method "Tests.TessClass.ShouldTimeout"
and I also added [assembly: CollectionBehavior(DisableTestParallelization = true)]
into my assembly configuration.
The test, however, still finishes successfully with the following output.
xUnit.net Console Runner v2.4.1 (64-bit Desktop .NET 4.5.2, runtime: 4.0.30319.42000)
Discovering: tests
Discovered: tests
Starting: tests
Finished: tests
=== TEST EXECUTION SUMMARY ===
tests Total: 1, Errors: 0, Failed: 0, Skipped: 0, Time: 15,151s
Did I miss something? Is the parallelization still not disabled and therefore Timeout
is ignored?
Looks like xUnit code has some internal SynchronizationContext usage, which results in all threads blocked when synchronous wait is executed inside a test - i.e. Thread.Sleep(). See source code
But, Timeout
works just fine if your test code is fully async
:
[Fact(Timeout = 1000)]
public async void ShouldTimeout()
{
await Task.Delay(3000);
}
So I see 2 options here for you: either create a bug for xUnit regarding synchronous code blocking breaking Timeout
behavior or rewrite your code to be async
if your code base allows it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With