如何在 Test Explorer 的 xUnit 中仅显示方法名称?

How can I show only method name in xUnit in Test Explorer?

如果我 运行 在 Visual Studio 中进行单元测试:

我只想显示方法名称。我看过 ,但那是基于 App.config。

如何在配置模型完全不同的 .NET Core 中做同样的事情?

根据 official docs,您可以使用名为

json 文件为您的 .Net Core 应用程序提供设置

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,显示名称将只是方法(没有 class 名称);如果将此值设置为 classAndMethod,默认显示名称将是完全限定的 class 名称和方法名称。
    JSON 架构类型:enum
    默认值:classAndMethod

  • 并行化程序集

  • parallelizeTestCollections

  • preEnumerateTheories

  • shadowCopy

编辑:正如您在文档中看到的那样,只有两个选项:classAndMethodmethod。根据 github 问题 #524,Xunit API 中的名称和名称空间与 class 没有区别,因此您需要找到解决方法。

例如,this answer方法:

public class MyFactAttribute : FactAttribute
{
    public MyFactAttribute([CallerMemberName] string memberName = null)
    {
        DisplayName = memberName;
    }
}

一些有用的链接: