【问题标题】:Execute tests based on Xunit filtered by traits in Teamcity根据 Teamcity 中的特征过滤的 Xunit 执行测试
【发布时间】:2014-02-15 00:16:30
【问题描述】:

我正在将我的项目从 NUnit 转移到 xUnit 测试框架。我们正在通过 MSBuild 任务在 TeamCity 中执行测试。我想按类别排除测试。在 NUnit 和 Teamcity 中,这很简单。

我将如何在 xUnit 中解决这个问题?

Msbuild 目标如下所示:

  <Target Name="xUnitTests">
    <xunit Assembly="$(SolutionDir)\Tests\bin\Debug\MyApp.Tests.exe" />
  </Target>

理想情况下,我想将Exclude="Category=database" 作为属性添加到&lt;xunit&gt; 元素,但这无效。

我快速浏览了 xUnit 源代码,并没有找到 msbuild runner 的这个选项。

在 msbuild runner 中忽略测试的任何其他替代方法?

【问题讨论】:

    标签: msbuild teamcity xunit xunit.net


    【解决方案1】:

    我将通过简单的示例来扩展 Josh Gallagher 的答案,说明我是如何做到的。假设您有以下测试:

    [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);
    }
    

    您的 msbuild 目标文件中可能包含以下内容:

    <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>
    

    【讨论】:

    • 在提出问题的时候,这不可用 - 正是这样尝试过,并且引发了有关无效 xml 的错误。
    • @trailmax,哦,我明白了。好吧,好消息是现在它对我来说工作得很好,所以他们可能会正确地修复(或实施)这个问题。
    【解决方案2】:

    在测试中使用 TraitAttribute,在 msbuild 文件中使用 Exec 任务,并使用带有 /-trait "Category=database" 参数的 xunit.console.clr4.exe 运行程序。

    另一种方法是不使用 msbuild,而是在 TeamCity 中创建一个额外的步骤,您可以在其中直接运行 xunit 控制台。您可以在 xunit 项目文件中指定程序集。这是我用于 TeamCity 和 XUnit.net 的解决方案。我将 xunit 项目文件保存在我的解决方案项目文件夹中,并手动将测试程序集添加到其中。

    【讨论】:

    • 注意,在 xunit 控制台运行程序 2 或更高版本中,按特征排除开关现在为 -notrait,如下所示,它将跳过使用类别数据库的测试:xunit.console.exe path\to\mytest.dll -notrait "Category=database"
    【解决方案3】:

    虽然不像 Josh 提到的那样基于 MSBuild,但我创建了一个 xunit + dotcover 元运行器,它支持包含和排除 xunit 特征、过滤器和通配符选择。这意味着您可以创建针对特定测试集的构建步骤。如果您只需要测试运行器部分,您也可以排除 dotcover 的部分。

    你可以在我的帖子中找到详细信息和来源:

    http://www.wwwlicious.com/2015/09/25/teamcity-dotcover-xunit-at-last/

    【讨论】:

    • 酷。我将在我使用 XUnit 做的下一个项目中给它一个 bash
    猜你喜欢
    • 1970-01-01
    • 2023-03-02
    • 2017-07-04
    • 1970-01-01
    • 2019-01-12
    • 1970-01-01
    • 2015-05-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多