3

If I run unit tests in Visual Studio:

  • If I use NUnit, Test Explorer shows the method names.
  • If I use xUnit, Test Explorer shows the fully qualified name including namespace, class name and method name. That's a bit too long.

I would like to show the method name only. I've seen that you can specify a setting in the App.config to show just the method name, but that is based on App.config.

How do I do the same thing in .NET Core, which has a completely different configuration model?

Community
  • 1
  • 1
Gigi
  • 28,163
  • 29
  • 106
  • 188

1 Answers1

6

According the official docs, you can provide the settings for your .Net Core application with json file named

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.

Supported configuration items are (The configuration elements are placed inside a top-level object):

  • appDomain

  • diagnosticMessages

  • longRunningTestSeconds

  • maxParallelThreads

  • methodDisplay

    Set this to override the default display name for test cases. If you set this to method, the display name will be just the method (without the class name); if this set this value to classAndMethod, the default display name will be the fully qualified class name and method name.
    JSON schema type: enum
    Default value: classAndMethod

  • parallelizeAssembly

  • parallelizeTestCollections

  • preEnumerateTheories

  • shadowCopy

Edit: as you can see in docs, there are only two options: classAndMethod and method. According the github issue #524, there is no difference from class name and namespace in Xunit API, so you need to find a workaround.

For example, this answer approach:

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

Some useful links:

  • [Proposal] Support Automatic "Pretty" Display Name for Test Cases, Issue #759
  • Pretty Display Name Implementation for Test Cases, PR #828
  • NuGet package with PR
VMAtm
  • 27,943
  • 17
  • 79
  • 125