如何订购属于一个测试集合但分布在多个测试类中的xUnit测试?

时间:2018-09-03 09:09:32

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

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

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

1 个答案:

答案 0 :(得分:1)

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

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

我过去的工作是使用部分类来组织需要按顺序排列到具有不同文件的同一个类中的测试,以便仍然可以将测试组织成不同的文件。

mat_list

# species_df$SPECIES: ALCEDO ATTHIS
#                WING    WINGPRI     WEIGHT      BEAK     TARSUS
# p_value   0.7273165  0.8382798  0.3180979 0.6450270  0.3856571
# t_value.t 0.3501749 -0.2048995 -1.0055505 0.4629014 -0.8733496
# ---------------------------------------------------------------------------------------- 
# species_df$SPECIES: CARDUELIS CARDUELIS
#                WING    WINGPRI    WEIGHT      BEAK    TARSUS
# p_value   0.5200729  0.8520463 0.3370721 0.8189008 0.1212502
# t_value.t 0.6470729 -0.1873091 0.9678003 0.2299977 1.5716422
# ---------------------------------------------------------------------------------------- 
# species_df$SPECIES: CHLORIS CHLORIS
#                 WING    WINGPRI     WEIGHT       BEAK    TARSUS
# p_value    0.1115453  0.5689228 0.94825726  0.5989776 0.9108546
# t_value.t -1.6129915 -0.5725928 0.06514506 -0.5284384 0.1124033
# ---------------------------------------------------------------------------------------- 
# species_df$SPECIES: ESTRILDA ASTRILD
#                 WING   WINGPRI    WEIGHT      BEAK    TARSUS
# p_value   0.09291222 0.7700545 0.6859697 0.1958938 0.6452502
# t_value.t 1.70719717 0.2935269 0.4062293 1.3054498 0.4624954
# ---------------------------------------------------------------------------------------- 
# species_df$SPECIES: FRINGILLA COELEBS
#                 WING   WINGPRI    WEIGHT      BEAK   TARSUS
# p_value   0.06157204 0.8636649 0.2183259 0.4757378 0.274626
# t_value.t 1.89924201 0.1723255 1.2416417 0.7170863 1.101813
# ---------------------------------------------------------------------------------------- 
# species_df$SPECIES: PARUS MAJOR
#                 WING   WINGPRI    WEIGHT       BEAK    TARSUS
# p_value   0.96688923 0.5857059 0.1140328  0.5055508 0.5747242
# t_value.t 0.04168846 0.5481212 1.6046303 -0.6694396 0.5643418
# ---------------------------------------------------------------------------------------- 
# species_df$SPECIES: SYLVIA ATRICAPILLA
#                 WING   WINGPRI    WEIGHT      BEAK     TARSUS
# p_value    0.4350621 0.5446387 0.7073097 0.3911381  0.7631614
# t_value.t -0.7851506 0.6091449 0.3770283 0.8628441 -0.3024993