Reputation: 5434
I'm using xUnit and I have same load tests which I'd like to execute only under certain conditions:
How can I achieve this? Is there any chance to accomplish this with xUnit, preferably without conditional compiles?
Upvotes: 2
Views: 1869
Reputation: 11
I think the easiest way to get around this if not the prettiest, is enclosing the test you want to "skip" locally is to enclose your test like this:
public class TestClass1
{
#if !DEBUG
[Fact]
#endif
public void Test1()
{
Assert.True(true);
}
}
This will skip the [Fact] attribute which triggers the test at run time, as long as you IDE or editor, or however you run your code, is set to build for "Debug", which is default for most.
A better way to handle this is probably using the xUnit [Fact(Skip = "Reason")] but I have had issues with making this work on runners on Gitlab sometimes.
And for your last case where you want it to run, you can just "run your code or tests" in "Release mode".
EDIT: In regards to your answer and my own suggestion of using the build in "skip" functionality for xUnit perhaps this post could be of help.
You could then create your own Attribute method to check the environment you run on:
https://josephwoodward.co.uk/2019/01/skipping-xunit-tests-based-on-runtime-conditions
Upvotes: 0