gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 12:03:48 25 4
gpt4 key购买 nike

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

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

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

// 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
}
}
}

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

如何防止 Visual Studio 测试资源管理器显示 .NET Standard 库中的测试?

最佳答案

Common 项目中的基类声明为 abstract

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

关于c# - 防止 Visual Studio Test Explorer 在共享的 .NET Standard 库中发现测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55995536/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com