VS2022中NUnit的TestCase添加TestName导致测试无法被发现
NUnit TestCase添加TestName参数导致VS2022测试无法发现的问题解决
问题现象
使用NUnit编写测试时,若给任意[TestCase]特性添加TestName参数且将其放在参数列表末尾,对应的测试会无法在VS2022测试资源管理器中被发现,测试运行计数器数值减少;移除TestName参数或调整其位置后,测试可恢复正常识别。
环境信息
- VS Enterprise 2022 v17.3.6
- .NET Framework v4.8.04084
- C# Tools 4.3.0-3.22470.13+80a8ce8d5fdb9ceda4101e2acb8e8eb7be4ebcea
- NuGet Package Manager 6.3.0
复现代码
可正常识别的测试代码
[TestCase(noneAur, ExpectedResult = 0, Description = "Finds nothing")] [TestCase(oneAur, ExpectedResult = 1, Description = "Finds only one aur in report.")] [TestCase(oneAurPlus, ExpectedResult = 1, Description = "Find one aur, ignores afr in report")] [TestCase(twoAURs, ExpectedResult = 2, Description = "Finds two aur's in report")] public int parseAUR(string source) { return ClaimStatusService.ProcessUndeliverableReport(source); }
添加TestName(末尾位置)后无法识别的代码
[TestCase(noneAur, ExpectedResult = 0, Description = "Finds nothing", TestName = "parseAUR(None)")] [TestCase(oneAur, ExpectedResult = 1, Description = "Finds only one aur in report.", TestName = "parseAUR(One)")] [TestCase(oneAurPlus, ExpectedResult = 1, Description = "Find one aur, ignores afr in report", TestName = "parseAUR(OnePlus)")] [TestCase(twoAURs, ExpectedResult = 2, Description = "Finds two aur's in report", TestName = "parseAUR(Two)")]
原因推测
该问题大概率是VS2022与NUnit测试适配器在解析特性命名参数时的兼容性Bug,当TestName作为最后一个命名参数时,解析逻辑出现异常,导致测试元数据无法被正确读取。目前在社区、NUnit及微软官网暂未找到相关公开问题报告。
解决方案
调整TestName参数在[TestCase]特性中的位置,避免将其放在参数列表末尾。示例:
[TestCase(noneAur, TestName = "parseAUR(None)", ExpectedResult = 0, Description = "Finds nothing")]
调整任意一个TestCase的参数位置后,所有测试均可正常被测试资源管理器发现。
内容的提问来源于Stack Exchange,提问作者Display name
相关产品推荐
相关产品推荐

