I have a set of tests with some setup required before each test. The setup requires me to run it async and I don't particularly want to put async code running in a constructor, which is recommended by xunit
public class Tests
{
private async Task SetupForEachTestAsync()
{
// await setup
}
public Tests()
{
SetupForEachTestAsync.GetAwaiter().GetResult();
}
[Fact]
public void Test1()
{
// My test
}
[Fact]
public void Test2()
{
// My test
}
}
Any recommendations on how I can improve this?
Implement xUnit's IAsyncLifetime
interface. It defines InitializeAsync
and DisposeAsync
which will be called immediately after construction and immediately before disposal, respectively.
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