One of the neat tricks that you can do in XUnit is to make your test more readible.
Here is one of my unit tests:
[Fact] | |
public async Task An_Authorized_User_Gets_All_MenuItems() | |
{ | |
//Removed test implementation | |
} |
And here is how this looks by default in the Test Explorer:
Adding a configuration file
Let’s now add a configuration file to improve the default behavior.
-
Add a new JSON file to the root of your test project. Name the file
xunit.runner.json
. -
Add a schema reference to get autocomplete behavior while editing the file:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters{ "$schema": "https://xunit.net/schema/current/xunit.runner.schema.json" } - Remark: In case of Visual Studio, this is not really necessary as it will recognize the file name and apply the correct schema automatically.
-
Tell the build system to copy the file into the build output directory. Edit your
.csproj
file and add the following:<ItemGroup> <Content Include="xunit.runner.json" CopyToOutputDirectory="PreserveNewest" /> </ItemGroup>
Update the configuration
Let’s now tweak the behavior by changing some settings in the configuration file:
{ | |
"$schema": "https://xunit.net/schema/current/xunit.runner.schema.json", | |
"methodDisplay": "method", | |
"methodDisplayOptions": "all" | |
} |
This will make our test list already more readable:
Remark: If you don’t see anything happening, check that you have the latest version of XUnit and XUnit.Runner.VisualStudio installed.
I invite you to check out the documentation for some other options you can tweak: https://xunit.net/docs/configuration-files