如何在xUnit中设置测试用例序列

Pan*_*aha 26 c# xunit.net

我在C#中编写了xUnit测试用例.该测试类包含很多方法.我需要按顺序运行整个测试用例.如何在xUnit中设置测试用例序列?

Kno*_*per 32

在xUnit 2.*中,这可以使用TestCaseOrderer属性来实现,以指定排序策略,该策略可用于引用在每个测试上注释的属性以表示订单.

例如:

订购策略

[assembly: CollectionBehavior(DisableTestParallelization = true)] 

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

        foreach (TTestCase testCase in testCases)
        {
            int priority = 0;

            foreach (IAttributeInfo attr in testCase.TestMethod.Method.GetCustomAttributes((typeof(TestPriorityAttribute).AssemblyQualifiedName)))
                priority = attr.GetNamedArgument<int>("Priority");

            GetOrCreate(sortedMethods, priority).Add(testCase);
        }

        foreach (var list in sortedMethods.Keys.Select(priority => sortedMethods[priority]))
        {
            list.Sort((x, y) => StringComparer.OrdinalIgnoreCase.Compare(x.TestMethod.Method.Name, y.TestMethod.Method.Name));
            foreach (TTestCase testCase in list)
                yield return testCase;
        }
    }

    static TValue GetOrCreate<TKey, TValue>(IDictionary<TKey, TValue> dictionary, TKey key) where TValue : new()
    {
        TValue result;

        if (dictionary.TryGetValue(key, out result)) return result;

        result = new TValue();
        dictionary[key] = result;

        return result;
    }
}
Run Code Online (Sandbox Code Playgroud)

属性

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

    public int Priority { get; private set; }
}
Run Code Online (Sandbox Code Playgroud)

测试用例

[TestCaseOrderer("FullNameOfOrderStrategyHere", "OrderStrategyAssemblyName")]
public class PriorityOrderExamples
{
    [Fact, TestPriority(5)]
    public void Test3()
    {
        // called third
    }

    [Fact, TestPriority(0)]
    public void Test2()
    {
      // called second
    }

    [Fact, TestPriority(-5)]
    public void Test1()
    {
       // called first
    }

}
Run Code Online (Sandbox Code Playgroud)

xUnit 2.*在这里订购样品

  • 只有在我添加了程序集属性后,这才奏效... [程序集:CollectionBehavior(DisableTestParallelization = true)] (2认同)

And*_*iff 19

Testpriority:在底部页面.

[PrioritizedFixture]
public class MyTests
{
    [Fact, TestPriority(1)]
    public void FirstTest()
    {
        // Test code here is always run first
    }
    [Fact, TestPriority(2)]
    public void SeccondTest()
    {
        // Test code here is run second
    }
}
Run Code Online (Sandbox Code Playgroud)

顺便说一下,我现在也有同样的问题.是的,这不是一个干净的艺术..但QA想要一个手动测试..所以具有特定订单的自动测试已经是他们的一个重大飞跃..(咳嗽)是的,它不是真正的单元测试..


Mar*_*oPT 10

如果您确实需要优先考虑您的测试(可能不是您的单元测试),您可以使用Xunit.Priority。我已经将它用于一些集成测试,并且工作得非常好且简单,无需为简单的案例场景编写优先级类的开销


Rub*_*ink -5

根据设计,你不能。它是故意随机的,以防止任何人通过欲望或意外获得其中之一。

随机性仅适用于给定的测试类,因此您可以通过包装要控制嵌套类内部顺序的项目来实现您的目标 - 但在这种情况下,每当您一个类中有两个以上的测试方法。

如果您正在尝试管理固定装置或环境的构建,那么内置IUseFixture<T>机制可能是合适的。有关示例,请参阅xUnit 备忘单。

但你确实需要告诉我们更多关于你想要做什么的信息,否则我们只能进行推测。