Reputation: 35106
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?
Upvotes: 5
Views: 5751
Reputation: 1214
Although not based on MSBuild and like Josh mentioned, I have created an xunit + dotcover meta-runner that supports including and excluding xunit traits, filters and wildcard selections. This means you can create build steps that target specific sets of tests. You could also exclude the parts for dotcover if you only need the test runner part.
You can find the details and source in my post:
http://www.wwwlicious.com/2015/09/25/teamcity-dotcover-xunit-at-last/
Upvotes: 1
Reputation: 6465
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>
Upvotes: 5
Reputation: 5329
Use the TraitAttribute
in the tests, an Exec
task in the msbuild file and the xunit.console.clr4.exe runner with the /-trait "Category=database"
argument.
An alternative is not to use msbuild but instead create an extra step in TeamCity where you run the xunit console directly. You can specify the assemblies in an xunit project file. This is the solution I used to employ with TeamCity and XUnit.net. I kept the xunit project file in my solution items folder and manually added the test assemblies to it.
Upvotes: 2