升级MSTest与Microsoft.NET.Test.Sdk后,带DataRow特性的测试无法执行
升级MSTest与Microsoft.NET.Test.Sdk后,带DataRow特性的测试无法执行
我目前在用Visual Studio 17.12.5,最近遇到了一个头疼的问题——把MSTest包从3.7.3升级到3.8.0,同时把Microsoft.NET.Test.Sdk从17.12.0升级到17.13.0之后,我的绝大部分测试都被忽略了。更准确地说,Visual Studio的测试资源管理器里,大部分测试都带着黄色警告标记,只有大概2%的测试能正常执行。
升级后的项目包引用配置如下:
[...] <ItemGroup> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" /> <PackageReference Include="MSTest" Version="3.8.0" /> <PackageReference Include="coverlet.collector" Version="6.0.4"> [...]

但只要回滚到MSTest 3.7.3和Microsoft.NET.Test.Sdk 17.12.0,所有测试又都能正常执行了,回滚后的配置如下:
[...] <ItemGroup> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" /> <PackageReference Include="MSTest" Version="3.7.3" /> <PackageReference Include="coverlet.collector" Version="6.0.4"> [...]
另外我用Fine-Code-Coverage做代码覆盖率统计。吐槽一句:最坑人的是第一眼看上去所有测试都是绿色的,得仔细看才发现有10152个测试被跳过,只有156个正常执行,这体验真的太糟心了!
后来我仔细排查了一下,发现带[DataRow(...)]特性的测试都会被跳过,比如下面这个测试:
/// <summary> /// Test EnumMember-Helper function /// </summary> /// <param name="name">The name.</param> /// <param name="peExp">The expected result</param> [DataTestMethod()] [DataRow(nameof(intProp), PropEnum.intProp)] [DataRow(nameof(boolProp), PropEnum.boolProp)] [DataRow(nameof(enumProp), PropEnum.enumProp)] [DataRow(nameof(objectProp), PropEnum.objectProp)] [DataRow(nameof(stringProp), PropEnum.stringProp)] [DataRow("BlaBla", null)] public void EnumMemberTest(string name, PropEnum? peExp) { Assert.AreEqual(peExp, typeof(PropEnum).EnumMember(name)); }
而不带[DataRow]的普通测试(仅用[TestMethod]标记的)则能正常执行,比如这个:
/// <summary> /// Test EnumMemberTest, to thow exception. /// </summary> [TestMethod()] public void EnumMemberTest2() { Assert.ThrowsException<ArgumentException>(() => typeof(string).EnumMember("Hallo")); }
这个问题在所有目标框架下都会出现,包括.net4.6.2到.net4.8.1,以及.net6.0到.net9.0。不知道有没有其他朋友遇到过同样的情况,或者知道怎么解决这个问题?
备注:内容来源于stack exchange,提问作者Joe Care
相关产品推荐
相关产品推荐

