Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute tests based on Xunit filtered by traits in Teamcity

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?

like image 312
trailmax Avatar asked Feb 15 '14 00:02

trailmax


1 Answers

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>
like image 143
Sevenate Avatar answered Oct 04 '22 02:10

Sevenate