- Knowledge for Developers.
Follow Us
Home Backend .NET + xUnit categorizing and organizing tests with Traits

.NET + xUnit categorizing and organizing tests with Traits

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:

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.

Written by
Renato Groffe

Worked as a consultant in systems development activities for over 15 years. Microsoft MVP (Most Valuable Professional) and MTAC (Multi-Plataform Technical Audience Contributor) program participant. Bachelor in Information Systems, specialization in Software Engineering, and MBA in Business Intelligence. He is also a lecturer and technical author on portals and specialized magazines, focusing on Microsoft technologies and good practices in the software area.

Leave a comment

Deixe um comentário

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *

Related Articles


How to Create a Skill for Amazon’s Virtual Assistant Alexa

If you didn’t know, it is not necessary for an Amazon Echo...


The APIs role in a 5G world

5G is about to revolutionize how we connect and use technology daily....


EF Core using AsNoTracking with Identity Resolution

Today we will see the consequences of using AsNoTracking with the Identity...


Understand key features added in ASP.NET Core 7.0

Version 7.0 of the .NET platform brought many new features in this...