Adding Test Categories with xUnit
By Harry Bellamy
- 1 minutes read - 145 wordsWhen writing unit tests it can be helpful to group unit tests using categories. For example, if I have both unit and integration tests in the same solution I can group the unit tests by adding a trait:
using Xunit;
namespace MyNamespace
{
[Trait("Category", "Unit")]
public class MyUnitTests
{
[Fact]
public void MyTest()
{
...
}
}
}
This is fine for small projects, but if there are numerous test classes in a project it can become cumbersome and easy to miss one or two out.
Assembly-Level Traits
Fortunately it is possible to apply a trait to an entire test project.
At the root of your test project create a folder called Properties
.
In that folder, create a file named AssemblyInfo.cs
. Copy the following content into the file:
using Xunit;
[assembly: AssemblyTrait("Category", "Unit")]
Now all your tests in that project will have the Unit
category.