xUnit gives us DisplayName
property for [Fact]
attribute to specify how our test should be displayed in test explorer in Visual Studio. For example:
public class BooksIntegrationTests
{
[Fact(DisplayName = "GET /api/books returns http status code 200")]
public Task Get_Books_ReturnsCollectionOfBooks()
{
// ... test code
}
}
So instead of displaying method name Get_Books_ReturnsCollectionOfBooks
test explorer now shows more friendlier test name GET /api/books returns http status code 200
.
But is there an equivalent way for specifying DisplayName
on a class that contains these tests? I'd like test explorer to show something like Integration tests for Books API resource
instead of just class name BooksIntegrationTests
.
Is there a way to do this via some xUnit class attribute?
You can use the class fixture feature of xUnit.net to share a single object instance among all tests in a test class. We already know that xUnit.net creates a new instance of the test class for every test.
xUnit uses the [Fact] attribute to denote a parameterless unit test, which tests invariants in your code. In contrast, the [Theory] attribute denotes a parameterised test that is true for a subset of data. That data can be supplied in a number of ways, but the most common is with an [InlineData] attribute.
The only way I have found to group xUnit tests under more friendly names in the Visual Studio Test Explorer window is to use the Traits feature, and then use the UI options in Test Explorer to group tests by traits instead of by Class. Example code:
public class BooksIntegrationTests
{
[Xunit.TraitAttribute("Category", "Integration tests for Books API resource")]
[Fact(DisplayName = "GET /api/books returns http status code 200")]
public void Get_Books_ReturnsCollectionOfBooks()
{
// ... test code
}
[Trait("Category", "Integration tests for Books API resource")]
[Fact(DisplayName = "GET /api/books/book returns http status code 999")]
public void Get_BookByISBN_ReturnsSingleBook()
{
// ... test code
}
}
Then your Test Explorer will show like this:
The first circled-in-red button is the "Show Test Hierarchy" button, and it should be deselected. Just to the right of it is the "Group By..." dropdown, and click it and select to group by "Traits". I used a trait called "Category" to group the two xUnit tests, and invented another test that returns a single book rather than a collection of books. Using this view, both related tests show under the "Category: Integration Tests for Books API Resource".
The above is for VS2017. I expect VS2019 will be same/similar.
I did not find a way to do exactly what you were trying to do, to make it so the Group By "Class" shows more friendly names in the Test Explorer Window. I tried putting in attributes in the AssemblyInfo.cs file, changing the project properties, and nothing worked to change the Class view. There might be a way, but I did not find it. Hope this helps get you closer to your goal.
P.S. The "Trait" and "TraitAttribute" attributes appear to be equivalent and interchangeable. I showed both forms in the code to illustrate that point. It seems that the "Trait" one is perhaps the newer, preferred form of the attribute, since Visual Studio uses that form if you choose to "Simplify" the name.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With