I'm moving my projects from NUnit to xUnit test framework. We are executing tests in TeamCity via MSBuild task. I would like to exclude tests by categories. In NUnit and Teamcity this is simple.
How would I go about this in xUnit?
Msbuild target looks like this:
<Target Name="xUnitTests">
<xunit Assembly="$(SolutionDir)\Tests\bin\Debug\MyApp.Tests.exe" />
</Target>
Ideally I'd like to add Exclude="Category=database"
as an attribute to <xunit>
element, but this is not valid.
I quickly looked through xUnit source code and did not find this option for msbuild runner.
Any other alternatives to ignore tests by traits in msbuild runner?
I'll just expand the Josh Gallagher answer a bit with simple example of how I do it. Assuming that you have the following tests:
[Fact]
[Trait("Category", "Integration")]
public async Task Test_for_long_running_operation()
{
var someClass = new SomeClass();
int result = await someClass.LongRunningOperationAsync()
Assert.Equal(5, result);
}
[Fact]
[Trait("Category", "Unit")]
public void Test_for_quick_operation()
{
var someClass = new SomeClass();
int result = someClass.GetSomeNumber()
Assert.Equal(3, result);
}
you could have the following in your msbuild target file:
<Target Name="xUnitTests">
<!-- For debug builds: skipping long integration tests -->
<xunit Condition="'$(Configuration)' == 'Debug'"
ExcludeTraits="Category=Integration"
Assembly="$(SolutionDir)\Tests\bin\Debug\MyApp.Tests.exe" />
<!-- For release builds: run them all -->
<xunit Condition="'$(Configuration)' == 'Release'"
Assembly="$(SolutionDir)\Tests\bin\Debug\MyApp.Tests.exe" />
</Target>
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