Using xUnit 2.4.1, I'm looking for a way to make it do my twisted bidding when it comes to dynamically skipping integration tests.
I'm aware xUnit is not designed for integration testing, but I have no intention to use xUnit for some tests and Not-xUnit for other tests. I think the distinction between unit and integration tests is pretty useless for a test runner.
With that out of the way, I have some tests that depend on a database. I added traits to the tests as follows.
public class Some_database_interaction
{
[Fact]
[Trait("Demand", "SqlServer")]
public async Task One_thing_is_inserted()
{
using var connection = await Connect();
var applicationService = new SomeService(connection);
var resultCount = await applicationService.DoTheImportantThing("some args", true);
Assert.Equal(1, resultCount);
}
}
Currently I'm using a command to skip tests that depend on the database when I don't want them to run.
dotnet test --filter Demand!=SqlServer
This is fine for use in a CI pipeline, but I cannot expect team members to type this command every time.
Instead I want some sort of way to detect capabilities and then skip tests if a demand is not satisfied. I don't know how to do that, though.
So far my best attempt is to add noise to my tests in the form of catching the error and short-circuiting the test.
SqlConnection connection;
try
{
connection = await Connect();
}
catch
{
// Exit the test
return;
}
using connection;
var applicationService = new SomeService(connection);
But this has the undesired side-effect of showing test as successful instead of skipped.
Any hints on where to go from here? I know xUnit 3 will have Assert.Skip() but for now I am stuck on xUnit 2.