One little-known feature of the xUnit.net testing framework is the ability to write test method names in a specific way and then have them converted to a ‘pretty’ version for example in Visual Studio Test Explorer.
Take the following test method:
using ClassLibrary1;
using Xunit;
namespace XUnitTestProject2
{
public class CalculatorShould
{
[Fact]
public void Add2PositiveNumbers()
{
var sut = new Calculator();
sut.Add(1);
sut.Add(1);
Assert.Equal(2, sut.Value);
}
}
}
By default, this will look like the following screenshot in Visual Studio Test Explorer:
The first thing that can be modified to to simplify the test method name display to only display the test method name and not the preceding namespace and class name, for example “XUnitTestProject2.CalculatorShould.Add2PositiveNumbers” becomes more simply “Add2PositiveNumbers” by making a simple configuration change.
Displaying Only Test Method Names in xUnit.net Tests
To control the rendering of method names in xUnit.net, the first thing to do is add a new file called “xunit.runner.json” to the root of the test project and set the Copy To Output Directory property to Copy if newer. This will make this file copy to the output bin directory. Once this is done, if you open the project file you should see something like:
<ItemGroup>
<None Update="xunit.runner.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
Next, modify the json file to the following:
{
"$schema": "https://xunit.net/schema/current/xunit.runner.schema.json",
"methodDisplay": "method"
}
Notice in the preceding json configuration the methodDisplay has been set to “method”, this will prevent the namespace and class being prepended to the method name in Test Explorer.
Now if you head back to Test Explorer you should see the following:
Enabling Pretty Method Names in xUnit.net
In addition to shortening test method name display we can also make use of xUnit.net’s “pretty method display”.
To enable this feature, modify the json configuration file and add the "methodDisplayOptions": "all" configuration as follows:
{
"$schema": "https://xunit.net/schema/current/xunit.runner.schema.json",
"methodDisplay": "method",
"methodDisplayOptions": "all"
}
Now the previous test can be renamed to “Add_2_positive_numbers” as follows:
[Fact]
public void Add_2_positive_numbers()
{
var sut = new Calculator();
sut.Add(1);
sut.Add(1);
Assert.Equal(2, sut.Value);
}
In test explorer this test method will show up as “Add 2 positive numbers” as the following screenshot shows:
You can use other items in the test method name, for example you can use the monikers eq, ne, lt, le, gt, ge that get replaced with =, !=, <, <=, >, >= respectively, for example a test name of “Have_a_value_eq_0_when_multiplied_by_zero” would be displayed as “Have a value = 0 when multiplied by zero”. Here the eq has been replaced with =.
You can also use ASCII or Unicode escape sequences, for example the test name “Divide_by_U00BD” gets displayed as “Divide by ½” and the test “Email_address_should_only_contain_a_single_U0040” gets displayed as “Email address should only contain a single @”, or “The_U2211_of_1U002C_2_and_3_should_be_6” becomes “The ∑ of 1, 2 and 3 should be 6”:
You could also combine the "methodDisplay": "classAndMethod" to create something like and the following:
namespace Given_a_cleared_calculator
{
public class when_a_number_gt_0_is_added
{
[Fact]
public void then_the_value_should_be_gt_0()
{
// etc.
}
[Fact]
public void then_the_value_should_eq_the_one_added()
{
// etc.
}
}
}
This would produce the following tests in Test Explorer:
If you want to learn more about writing tests with xUnit.net check out my Pluralsight course today.
SHARE: