在XUnit测试项目中,我希望跳过一个必须在DotMemory单元下运行的理论,当测试不是在DotMemory单元下运行时。
目前,我正在使用dotMemoryApi强制该理论失败,如下所示:
[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
}这是可行的,但我更希望在不使用DotMemory单元运行时跳过该理论,而不是失败。
我尝试过SkippableFact nuget包装,但在DotMemory Unit下运行时它失败了,但有以下例外:
DotMemoryUnitException : DotMemoryUnitController TestStart()和TestEnd()方法是从外部调用的-->测试方法:--如果您使用的单元测试运行程序没有现成的支持,那么可能是从>DotMemoryUnitController TestStart()和TestEnd()方法之外调用的,或者这些方法>是按错误的顺序调用的。
我还尝试过扩展XUnit.TheoryAttribute类,如下所示,这确实会导致理论被跳过,但不幸的是,即使在使用dotMemoryUnit运行时,它也跳过了测试。
public sealed class IgnoreOnDotMemoryNotEnabledTheory : TheoryAttribute
{
public IgnoreOnDotMemoryNotEnabledTheory() {
if(!dotMemoryApi.IsEnabled)
{
Skip = "Ignored due to DotMemory API not enabled"
}
}
}有什么想法吗?
发布于 2019-09-18 23:27:48
结果是,SkippableTheory属性包含在SkippableFact NuGet库中。我一定是错过了它,因为它不在自述项目中。
使用示例:
[SkippableTheory]
[MemberData(nameof(SomeTestData)]
public void MyTheory(object someData)
{
skip.IfNot(dotMemoryApi.IsEnabled);
// otherwise, proceed with dotMemory unit test calls
.
.
.发布于 2019-07-31 14:37:34
在这种情况下,当我有“仅内存”测试(没有任何意义的w/o dotMemory单元)时,我用一个类别(例如,类别(“MemoryOnly”)标记它们,并在运行所有测试时排除该类别。我使用NUnit,但肯定没有太大的区别。
https://stackoverflow.com/questions/57235446
复制相似问题