如何配置XUnit以在Visual Studio 2015测试资源管理器中仅显示方法名称?

Way*_*rch 41 xunit.net test-explorer visual-studio-2015

xunit.runner.visualstudio在Visual Studio 2015中使用2.0.1版时,测试的名称显示为完全限定.有没有办法让测试只显示方法名称?

考虑以下测试: -

namespace MySolution.Tests
{
    public class MyTestClass
    {
        [Fact]
        public void ClassUnderTest_WhenDefaultConstructorUsed_SomePropertyIsNotNull()
        {
            *... test code in here*
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在测试资源管理器中显示为: -

MySolution.Tests.MyTestClass.ClassUnderTest_WhenDefaultConstructorUsed_SomePropertyIsNotNull
Run Code Online (Sandbox Code Playgroud)

使用MSTest/VSTest,这将显示为: -

ClassUnderTest_WhenDefaultConstructorUsed_SomePropertyIsNotNull
Run Code Online (Sandbox Code Playgroud)

lor*_*phd 61

你也可以用json添加它.

在测试项目的根目录中添加一个名为"xunit.runner.json"的文件.

右键单击文件属性.选择"Copy if newer"复制到Output目录.

然后在文件中输入这个json:

{
  "methodDisplay": "method"
}
Run Code Online (Sandbox Code Playgroud)

  • 具体来说,这个__works for net core__也是. (4认同)
  • 请注意:对我来说,还需要重新打开项目才能使更改生效. (2认同)
  • 我可以注意到这个 JSON 方法适用于 Xamarin IOS Test Runner!:) app.config 也没有出现。 (2认同)

Bra*_*son 60

xunit.methodDisplay在您的App.config文件中设置.

<configuration>
  <appSettings>
    <add key="xunit.methodDisplay" value="method"/>
  </appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)

取自http://xunit.github.io/docs/configuring-with-xml.html

  • 我已经尝试使用dnx runner将'methodDisplay'配置选项设置为'method'.我按照这个文档http://xunit.github.io/docs/configuring-with-json.html来配置dnx runner.我的问题是,测试资源管理器中显示的名称仍然是[类].[方法],即使我期望它是[方法].当在控制台中使用'dnx test'命令运行测试时,它似乎确实按预期工作. (6认同)
  • @Impero更改了'App.config`后,关闭VS,转到`%TEMP%\ VisualStudioTestExplorerExtensions`,删除`xunit ...`文件夹并启动VS. (2认同)
  • 对于.NET Core,您需要使用https://xunit.github.io/docs/configuring-with-json.html。但是,复制文件存在问题:您需要确保实际更改测试,以便它将完全重新编译,然后您才能看到更改!当然,还将JSON文件的复制设置设置为“始终复制” (2认同)