防止Visual Studio Test Explorer在共享的.NET Standard库中发现测试

时间:2019-05-05 19:19:56

标签: c# visual-studio xunit

我有3个测试项目,具有以下依赖关系层次结构:

Tests.Common (.NET Standard library)
    Tests.DotNetCore (.NET Core library)
    Tests.Wpf (.NET Framework library)

想法是将测试方法定义为调用RunTests中的Tests.Common方法:

// using xUnit
// using System.Linq.Expressions
// using static System.Linq.Expressions.Expression

namespace Tests.Common {
    public class TestBase {
        protected virtual void RunTest(Expression expr, string result) { ... }
    }
    public class Constructed {
        [Fact]
        public void ConstructBinary() => 
            RunTest(
                Add(Constant(5), Constant(42)),
                "5 + 42"
            );
    }
}

然后在Tests.Wpf中,通过覆盖RunTests方法,可以针对仅UI VM类测试相同的表达式:

// using ExpressionToString.Wpf

namespace Tests.Wpf {
    public class Constructed : Tests.Common.Constructed {
        public override void RunTest(Expression expr, string result) {
            var vm = new ExpressionVM(expr);
            // at this point, all I want to test is that the VM class is created successfully
        }
    }
}

我的问题是,即使无法运行.NET Standard库中的xUnit测试,Visual Studio Test Explorer也会在Tests.Common中发现测试方法。 they require a platform-specific library(这就是为什么我有一个Tests.DotNetCore测试项目,以及那里的附加继承链的原因。)

如何防止Visual Studio Test Explorer在.NET Standard库中显示测试?

2 个答案:

答案 0 :(得分:1)

Common项目中将基类声明为abstract

这应该使测试资源管理器将基类排除为测试类,并识别所有后代类中的测试方法。

答案 1 :(得分:0)

不要在它们上设置[Fact]属性,而只能在实际实现上设置。

测试发现逻辑基于在测试项目中具有xUnit支持的签名的方法上的[Fact]属性匹配。不要在顶级实现中使用[Fact]属性,而只能在用于运行实际测试的替代方法中使用。这将阻止在Visual Studio Test Explorer和xUnit运行器中的任何级别上检测到它们。如果要直接重用顶级方法,请重写它并在正文中调用base.RunTest()。

相关问题