Timing Out Tests in C# & .NET
[C#, .NET, Testing]
In unit testing, there are scenarios where you want to exit your test run early.
Take this simple test, in the xUnit framework:
public async Task SampleTest()
{
await Task.Delay(TimeSpan.FromSeconds(15));
Assert.True(true);
}
This is a test with a built-in 15 second delay to simulate work.
This runs in a pretty straightforward manner.

Suppose you then have a time threshold above which you want a test to fail for taking too long.
Typically, you’d do this within the test, like so:
[Fact(Timeout = 10_000)]
public async Task SampleTest()
{
await Task.Delay(TimeSpan.FromSeconds(15));
Assert.True(true);
}
Here, I am indicating that I want the test to time out after 10 seconds using the Timeout attribute, which accepts a delay in milliseconds.
The command-line test runner returns the following:

An IDE test runner will return something similar:

TLDR
The xUnit Timeout attribute lets you set a limit for how long a test can run; otherwise, it fails with a timeout.
The code is in my GitHub.
Happy hacking!