Lesson 22 of 93 · 5 min read · Updated
Trait and Skip in xUnit: Categorizing Tests and Controlling What Runs
On this page
As a test suite grows, you need to run parts of it: only the fast tests on every commit, only the shipping tests while you work on shipping code. xUnit handles this with [Trait], which attaches name-value labels to tests that the runner can filter on. Sometimes you also need to switch a test off for a while, which is what Skip does. This lesson shows both, and the rules that keep a skipped test from turning into dead code.
Technical Requirements
- .NET 11 SDK
- xunit 2.9.3
- xunit.runner.visualstudio 4.0.1 and Microsoft.NET.Test.Sdk 18.12.0
- The
ShippingCalculatorclass from the earlier lessons
Labeling Tests with Trait
[Trait("Name", "Value")] adds a label to a test. A trait on the class applies to every test in it. A trait on a method applies only to that method, and it adds to the class-level traits instead of replacing them.
M03.DataDriven.Tests/TraitAndSkipTests.cs
Both tests carry Category=Fast from the class. Calculate_IsFastAndDeterministic also carries Topic=Shipping. Trait names and values are plain strings, so xUnit does not enforce any vocabulary. Your team does. Agree on a short list, such as Category with the values Fast, Slow and Integration, and use it everywhere. Two spellings of the same idea, like Fast and Quick, break filters without any warning.
The traits also show up in Test Explorer, where you can group tests by trait.
Filtering Tests on the Command Line
dotnet test --filter selects tests by trait. The syntax is Name=Value, Name!=Value, and you can combine conditions with & (and) and | (or).
A typical CI setup runs the fast unit tests on every push with Category=Fast and runs the slow or integration tests in a separate, less frequent job. The filter can also match on the test name with FullyQualifiedName~Shipping, but traits are more stable than names: renaming a test does not change which pipeline runs it.
Use traits for groups that drive decisions, such as which pipeline stage runs a test. Do not add a trait for every feature or ticket. A label that nobody filters on is noise.
Skipping a Test
Sometimes a test must not run for a while. The rule it checks may be about to change, or it may depend on a system that is down. Set the Skip property on [Fact] or [Theory] to a reason string.
M03.DataDriven.Tests/TraitAndSkipTests.cs
A skipped test is not executed, but it is not hidden either. The runner reports it as skipped, together with the reason. In the dotnet test summary it appears in its own count, next to passed and failed. That visibility is the main advantage over the alternatives. Commenting out a test or deleting the [Fact] attribute makes the test disappear from every report, and nobody remembers to bring it back.
Pitfall: Every Skip Needs an Owner and an Exit Condition
A skip with a reason like "broken" or "TODO" is a problem waiting to happen. Months later nobody knows why the test was switched off or whether it is safe to turn it back on. At that point the test is dead code that still looks like coverage.
The skip message in the example contains three things:
- A reference:
ISSUE-482links to a ticket where the full context lives. - An exit condition: the tariff changes in 2026 Q2. After that date, the test should be updated and re-enabled.
- An owner:
@shipping-teamis responsible for bringing the test back.
Treat these three parts as a required format for every skip in the code base. Some teams go further and add a check in code review or CI that rejects skip messages without a ticket number.
Also keep the number of skipped tests small. If the skipped count in your report keeps growing, the skips are hiding real problems. Review them regularly: fix the test, delete it, or confirm that the reason is still valid.
In xUnit 2, Skip is a fixed attribute value, so the decision is made at compile time. xUnit v3 adds ways to skip at run time, for example based on the operating system. Even then, the same rule applies: the reason must say why and until when.
Summary
[Trait("Name", "Value")]labels tests; class-level traits apply to every test in the class.dotnet test --filter "Name=Value"runs only the matching tests, which lets CI split fast and slow suites.- Agree on a small, fixed set of trait names and values so filters stay reliable.
Skip = "reason"disables a test but keeps it visible in the report, unlike commenting it out.- Every skip message needs a ticket reference, an exit condition and an owner.