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

问题描述 投票:21回答:4

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

c# xunit.net
4个回答
29
投票

在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;
    }
}

属性

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

    public int Priority { get; private set; }
}

测试用例

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

}

xUnit 2. *订购样品here


19
投票

Testpriority:在this页面的底部。

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

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


1
投票

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

随机性仅适用于给定的Test类,因此您可以通过包装要控制嵌套类内部顺序的项目来实现目标 - 但在这种情况下,无论何时您仍然会以随机顺序结束在一个班级中有两个以上的测试方法。

如果您正在尝试管理灯具或上下文的构建,内置的IUseFixture<T>机制可能是合适的。有关示例,请参阅xUnit Cheat Sheet

但你真的需要告诉我们更多关于你要做什么的事情,或者我们只需要投机。


0
投票

如果您确实需要确定测试的优先级(可能不是您的单元测试),您可以使用Xunit.Priority。我已经将它用于一些集成测试,并且工作非常简单,没有必须编写优先级类的开销,对于简单的情况场景

© www.soinside.com 2019 - 2024. All rights reserved.