C# 如何对属于一个测试集合但分布在多个测试类中的xUnit测试进行排序?

C# 如何对属于一个测试集合但分布在多个测试类中的xUnit测试进行排序?,c#,unit-testing,integration-testing,xunit,xunit.net,C#,Unit Testing,Integration Testing,Xunit,Xunit.net,我有一些测试方法,它们分布在多个测试类中,但属于单个测试集合。我使用的是xUnit提供的ITestCaseOrderer,但它只对单个测试类中的测试方法进行排序 [AttributeUsage(AttributeTargets.Method)] public class TestPriorityAttribute : Attribute { public TestPriorityAttribute(int priority) { this.Priority = p

我有一些测试方法,它们分布在多个测试类中,但属于单个测试集合。我使用的是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 StandardControllerTests2
{
    public StandardControllerTests2(StandardIntegrationTestFixture standardIntegrationTestFixture)
    {

    }

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

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

我还有其他属于同一测试集合的测试类。当我运行测试时,它不会在集合中排序。如何使这些测试按同一集合中的顺序运行?

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

根据xUnit board上的一个问题,在同一个集合中跨类排序目前是不可能的

我所做的工作是使用分部类将需要排序的测试组织到同一个类中,并使用不同的文件,这样测试仍然可以组织到不同的文件中

[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));
    }
}
你能解释一下你为什么要这么做吗。这一切听起来都非常错误-我相信你会同意,你最终会得到一个更好的解决方案,因为你不再需要这种级别的排序;)
[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));
    }
}