In an XUnit test project, I wish to skip an theory that must be run with DotMemory Unit when the test is not run under DotMemory Unit.
Currently I am using dotMemoryApi to force the theory to fail, as per the following snippet:
[Theory]
[MemberData(nameof(SomeTestData)]
public void MyTheory(object someData)
{
if (!dotMemoryApi.IsEnabled)
{
Assert.True(false, "DotMemory API not enabled");
}
// otherwise, proceed with dotMemory unit test calls
}
This works, but I would prefer that the theory be skipped when not ran with DotMemory Unit, rather than fail.
I've tried the SkippableFact nuget package, but it fails when running under DotMemory Unit with the following exception:
DotMemoryUnitException : dotMemory Unit methods were called from outside a >test method: - If you work with a unit test runner that is not supported out of the box, >then probably calls to dotMemory Unit were made from outside the >DotMemoryUnitController TestStart() and TestEnd() methods or these methods >were called in the wrong order. Learn more about the DotMemoryUnitController >class: https://www.jetbrains.com/help/dotmemory-unit/3.0/Working_with_Unsupported_Unit_Testing_Frameworks.html
I've also tried extending the XUnit.TheoryAttribute class, as below, which does cause the theory to be skipped, but unfortunately it skips the test even when run with dotMemoryUnit.
public sealed class IgnoreOnDotMemoryNotEnabledTheory : TheoryAttribute
{
public IgnoreOnDotMemoryNotEnabledTheory() {
if(!dotMemoryApi.IsEnabled)
{
Skip = "Ignored due to DotMemory API not enabled"
}
}
}
Any thoughts?
It turns out that there is a SkippableTheory attribute included in the SkippableFact NuGet library. I must have missed it because it wasn't in the project Readme.
Example of usage:
[SkippableTheory]
[MemberData(nameof(SomeTestData)]
public void MyTheory(object someData)
{
skip.IfNot(dotMemoryApi.IsEnabled);
// otherwise, proceed with dotMemory unit test calls
.
.
.