2015-09-02 42 views
35

在Visual Studio 2015中使用xunit.runner.visualstudio版本2.0.1時,測試的名稱顯示完全限定。有沒有辦法讓測試只顯示方法名稱?如何將XUnit配置爲只顯示Visual Studio 2015測試資源管理器中的方法名稱?

考慮以下測試: -

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

在Test Explorer,這顯示爲: -

MySolution.Tests.MyTestClass.ClassUnderTest_WhenDefaultConstructorUsed_SomePropertyIsNotNull 

使用的MSTest/VSTest這將顯示爲: -

ClassUnderTest_WhenDefaultConstructorUsed_SomePropertyIsNotNull 
+0

見一個低[答案](https://stackoverflow.com/a/41643793/1739931)爲__net-core__。 – THBBFT

回答

51

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

<configuration> 
    <appSettings> 
    <add key="xunit.methodDisplay" value="method"/> 
    </appSettings> 
</configuration> 

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

+0

謝謝布拉德!不知道我是如何錯過了這一點,好奇的是它與這個主題不符: - https://github.com/xunit/xunit/issues/524 –

+0

請注意,這隻適用於測試跑步者的2.0+版本。請參閱http://xunit.github.io/docs/configuring-with-xml.html上的文檔。 – MiloDC

+5

我試過使用dnx轉輪將'methodDisplay'配置選項設置爲'method'。我遵循這個文檔http://xunit.github.io/docs/configuring-with-json.html配置dnx亞軍。我的問題是測試資源管理器中顯示的名稱仍然是[Class]。[Method],即使我期望它是[Method]。但是,在控制檯中使用'dnx test'命令運行測試時,它似乎能夠按預期工作。 –

43

兩者也可以使用JSON添加。

在測試項目的根目錄中添加一個名爲「xunit.runner.json」的文件。

用鼠標右鍵單擊文件,屬性。選擇「Copy if newer」複製到輸出目錄。

然後在文件中輸入此JSON:

{ 
    "methodDisplay": "method" 
} 
+1

現在,閱讀** Test Explorer **非常容易 - 非常棒!在VS 2017 RTM中爲我工作。 – SliverNinja

+1

在我意識到應該加入這個答案之前,我花了很少的時間訪問這個答案。我很抱歉。 – tia

+1

具體來說,這個__works for net core__也是。 – THBBFT

相關問題