【问题标题】:How to order xUnit tests belonging to one test collection but spread across multiple test classes?如何订购属于一个测试集合但分布在多个测试类中的 xUnit 测试?
【发布时间】:2018-09-03 09:09:31
【问题描述】:

我有一些测试方法分布在多个测试类中,但属于单个测试集合。我正在使用 xUnit 提供的 ITestCaseOrderer,但它仅对单个测试类中的测试方法进行排序。

[AttributeUsage(AttributeTargets.Method)]
public class TestPriorityAttribute : Attribute
{
    public TestPriorityAttribute(int priority)
    {
        this.Priority = priority;
    }

    public int Priority { get; }
}

我已经按照以下方式实现了我的优先排序器。

public class PriorityOrderer : ITestCaseOrderer
{
    public IEnumerable<TTestCase> OrderTestCases<TTestCase>(IEnumerable<TTestCase> testCases) where TTestCase : ITestCase
    {
        var sortedMethods = new Dictionary<int, TTestCase>();

        foreach (var testCase in testCases)
        {
            var attributeInfo = testCase.TestMethod.Method.GetCustomAttributes(typeof(TestPriorityAttribute).AssemblyQualifiedName)
                .SingleOrDefault();
            if (attributeInfo != null)
            {
                var priority = attributeInfo.GetNamedArgument<int>("Priority");
                sortedMethods.Add(priority, testCase);
            }
        }

        return sortedMethods.OrderBy(x => x.Key).Select(x => x.Value);
    }
}

我的第一个测试类是这样的。

[TestCaseOrderer("Integration.Tests.PriorityOrderer", "CompanyName.ProjectName.Integration.Tests")]
[Collection("StandardIntegrationTests")]
[Trait("Category", "Integration")]
public class StandardControllerTests1
{
    public StandardControllerTests1(StandardIntegrationTestFixture standardIntegrationTestFixture)
    {

    }

    [Fact, TestPriority(1)]
    public void TestMethod1()
    {
    }

    [Fact, TestPriority(2)]
    public void TestMethod2()
    {
    }
}

我的第二个测试类是这样的

[TestCaseOrderer("Integration.Tests.PriorityOrderer", "CompanyName.ProjectName.Integration.Tests")]
[Collection("StandardIntegrationTests")]
[Trait("Category", "Integration")]
public class StandardControllerTests2
{
    public StandardControllerTests2(StandardIntegrationTestFixture standardIntegrationTestFixture)
    {

    }

    [Fact, TestPriority(3)]
    public void TestMethod3()
    {
    }

    [Fact, TestPriority(4)]
    public void TestMethod4()
    {
    }
}

我还有其他测试类也属于同一个测试集合。当我运行测试时,它没有在集合中排序。我如何命令这些测试在同一集合中按顺序运行?

【问题讨论】:

  • 你能否解释一下为什么你想这样做。这一切听起来都非常错误——我相信你会同意你最终会得到一个更好的解决方案,你不需要这种级别的排序;)

标签: c# unit-testing integration-testing xunit xunit.net


【解决方案1】:

我在构建排序系统以与 xUnit 并行运行测试时遇到了同样的问题,同时还在他们自己的集合中运行了一些具有依赖关系的测试。

根据 xUnit 板上的问题 898,目前无法在同一集合中跨类进行排序。

我使用的解决方法是使用部分类将需要排序的测试组织到具有不同文件的同一类中,以便仍然可以将测试组织到不同的文件中。

[Trait("Order", "")]   
[TestCaseOrderer(DependencyOrderer.TypeName, DependencyOrderer.AssemblyName)]
public partial class OrderTests
{
    [Fact]        
    public void Test0()
    {
        Thread.Sleep(TimeSpan.FromSeconds(1));
    }

    [Fact]
    [TestDependency("Test1", "Test0")]
    public void Test3()
    {
        Thread.Sleep(TimeSpan.FromSeconds(1));
    }
 }

[Trait("Order", "")]
public partial class OrderTests
{
    [Fact]
    public void Test2()
    {
        Thread.Sleep(TimeSpan.FromSeconds(1));            
    }

    [Fact]
    [TestDependency("Test0")]
    public void Test1()
    {
        Thread.Sleep(TimeSpan.FromSeconds(1));
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多