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

标签 configuration .net-core xunit

如果我在 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

  • 并行化程序集

  • 并行化测试集合

  • 预枚举理论

  • 阴影复制

编辑:正如您在文档中看到的那样,只有两个选项:classAndMethodmethod .根据 github 问题 #524 , Xunit API 中的类名和命名空间没有区别,所以你需要找到一个解决方法。

例如,this answer方法:

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

一些有用的链接:

关于configuration - 如何在 Test Explorer 的 xUnit 中仅显示方法名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43103755/

相关文章:

c# - DateTime.TryParse() 和 DateTime.TryParseExact() 无法在 linux/macos 上解析日期并在 windows 上正常工作

c# - 如何更改 WCF 的默认配置?

.net - .NET/LINQ-SQL/ASP.NET 中的连接字符串 hell

javascript - 你的 .conkerorrc 是什么样子的?

c# - XUnit 测试可以从要测试的项目内运行吗?

c# - 只有 1 个 Main 方法的 XUnit 测试项目 : "Program has more than one entry point defined."

unit-testing - ExpectedException xunit .net core

linux - 在 Linux 中注册可由启动和停止命令控制的守护进程

.net-core - 将应用洞察跟踪日志记录添加到 .net 核心控制台应用程序

c# - 如何捕获 ExtendedSocketException?