49

When using xunit.runner.visualstudio version 2.0.1 in Visual Studio 2015, the names of the tests show up fully qualified. Is there a way for the tests to show only the method name?

Consider the following test: -

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

In the Test Explorer this shows as: -

MySolution.Tests.MyTestClass.ClassUnderTest_WhenDefaultConstructorUsed_SomePropertyIsNotNull

Using MSTest/VSTest this will show up as: -

ClassUnderTest_WhenDefaultConstructorUsed_SomePropertyIsNotNull
cleftheris
  • 4,626
  • 38
  • 55
Wayne Birch
  • 645
  • 6
  • 18

2 Answers2

72

You can also add it with json.

In the root directory of your test project add a file called "xunit.runner.json".

Right-click the file, properties. Select "Copy if newer" for copy to Output directory.

Then in the file enter this json:

{
    "methodDisplay": "method"
}

Note that you may1 require to restart the IDE in order to have the changes applied.

1 Visual Studio 2019 requires an IDE restart.

Yennefer
  • 5,704
  • 7
  • 31
  • 44
lorengphd
  • 1,140
  • 8
  • 16
  • 1
    So much easier to read in **Test Explorer** now - great find! Worked for me in VS 2017 RTM. – SliverNinja - MSFT May 09 '17 at 17:33
  • 1
    It took me few visits to this answer before I realised I should upvote this. My apologies. – tia Aug 02 '17 at 10:32
  • 2
    Please note: for me it also required to reopen project for changes to take effect. – shytikov Dec 16 '17 at 10:08
  • 2
    Can I just note that this JSON method works for the Xamarin IOS Test Runner! :) the app.config one did not appear too. – WickedW Feb 21 '18 at 14:30
  • It worked for me after "restart" Visual Studio 2019 Professional. I don't know why. – Jordan Ferr Nov 22 '19 at 20:04
  • This was only part of the answer for me. I'm using Visual Studio 2022. For this to work, I also had to add some configuration to the .csproj file. For specifics, see the [xUnit docs on configuration](https://xunit.net/docs/configuration-files). – Steve Nyholm Jan 05 '22 at 03:48
62

Set xunit.methodDisplay in your App.config file.

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

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

cleftheris
  • 4,626
  • 38
  • 55
Brad Wilson
  • 67,914
  • 9
  • 74
  • 83
  • Thanks Brad! Don't know how I missed that and curiously it's at odds with this thread: - https://github.com/xunit/xunit/issues/524 – Wayne Birch Sep 07 '15 at 11:05
  • Note that this works only for version 2.0+ of the test runner. See the documentation at http://xunit.github.io/docs/configuring-with-xml.html. – MiloDC Dec 09 '15 at 01:17
  • 6
    I've tried setting the 'methodDisplay' config option to 'method', using the dnx runner. I followed this doc http://xunit.github.io/docs/configuring-with-json.html for the configuration of the dnx runner. My problem is that the name shown in the Test Explorer is still [Class].[Method], even though I expected it to be [Method]. It does seem to work as expected when running tests using the 'dnx test' command in a console though. – Nikolaj Dam Larsen Mar 21 '16 at 03:43
  • 2
    @Impero after you changed the `App.config`, close VS, go to `%TEMP%\VisualStudioTestExplorerExtensions`, remove `xunit...` folders and start VS. – meze Apr 21 '16 at 05:26
  • 1
    Not working for me. Same as Nikolaj Dam Larsen. Tried what meze suggested. – VivekDev Aug 14 '16 at 17:25
  • If it is not working for you, try changing the **App.config** property **"Copy to output directory" to "Copy Always"**. Did the trick for me. – jeanadam Dec 21 '16 at 18:35
  • 3
    For .NET Core, you need to use https://xunit.github.io/docs/configuring-with-json.html However, there is an issue with copying the file: you need to make sure you actually change a test so it will fully recompile before you can see the changes! Of course, also set the JSON file's copy settings to "copy always" – Structed Mar 31 '17 at 12:37