When using xUnit implementation of tests in .NET, it is more than common to code numerous types of validations, thus guaranteeing the validity of the requirements analyzed by such projects. But different forms of checking bring the need to organize the results produced to simplify their visualization when the execution happens through Visual Studio.
xUnit, fortunately, has a feature to categorize tests, allowing you to organize them into specific groupings: this is possible thanks to Traits’ use! I even demonstrated this ability in a recent live on the .NET Channel (in Portuguese):
In the following example, we can observe the use of the Trait attribute with the ValidarConversaoMilhasParaKm (Miles – Conversion) and ValidarConversaoKmParaMilhas (Km – Conversion) methods:
In the following image, we visualize the results of these tests grouped by Traits from the Test Explorer window of Visual Studio 2022:
There is also the possibility to select what will be executed by clicking with the right mouse button on a Trait:
Or even filter by Traits via the command line, as in the case of the instruction:
dotnet test --filter "Milhas=Conversao"
In an example that only considered the conversion of Miles to Km (6 test cases), without any more code failures:
This example has been made available as a repository on GitHub:
https://github.com/renatogroffe/DotNet7_RC1-xUnit_Traits-FluentAssertions_ConversorDistancias
If you find this useful, please give a ⭐️ to support. I also invite you to follow me on GitHub!
*The content of this article is the author’s responsibility and does not necessarily reflect the opinion of iMasters.
Leave a comment