如果我在 Visual Studio 中运行单元测试:
- 如果我使用 NUnit,测试资源管理器会显示方法名称。
- 如果我使用 xUnit,测试资源管理器会显示完全限定名称,包括命名空间、类名和方法名。有点太长了。
我只想显示方法名称。我看过 you can specify a setting in the App.config to show just the method name , 但这是基于 App.config 的。
如何在配置模型完全不同的 .NET Core 中做同样的事情?
最佳答案
根据official docs ,您可以为您的 .Net Core
提供设置申请 json
文件名为
xunit.runner.json
or<AssemblyName>.xunit.runner.json
, where<AssemblyName>
is the name of your unit test assembly, without the file extension like.dll
or.exe
You should only need to use this longer name format if your unit tests DLLs will all be placed into the same output folder, and you need to disambiguate the various configuration files.
The assembly-specific filename takes precedence over the non-specific filename; there is no merging of values between files.
支持的配置项有(配置元素放在顶级对象中):
应用域
诊断消息
longRunningTestSeconds
最大并行线程数
方法显示
设置此项以覆盖测试用例的默认显示名称。如果将其设置为
method
,显示名称将只是方法(没有类名);如果将此值设置为classAndMethod
, 默认显示名称将是完全限定的类名和方法名。
JSON 架构类型:枚举
默认值:classAndMethod
并行化程序集
并行化测试集合
预枚举理论
阴影复制
编辑:正如您在文档中看到的那样,只有两个选项:classAndMethod
和 method
.根据 github 问题 #524
, Xunit API 中的类名和命名空间没有区别,所以你需要找到一个解决方法。
例如,this answer方法:
public class MyFactAttribute : FactAttribute
{
public MyFactAttribute([CallerMemberName] string memberName = null)
{
DisplayName = memberName;
}
}
一些有用的链接:
- [建议] 支持测试用例的自动“漂亮”显示名称,Issue
#759
-
Pretty
测试用例的显示名称实现,PR#828
- NuGet package with PR
关于configuration - 如何在 Test Explorer 的 xUnit 中仅显示方法名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43103755/