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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9210281/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 06:22:46  来源:igfitidea点击:

How to set the test case sequence in xUnit

c#xunit.net

提问by Pankaj Saha

I have written the xUnit test cases in C#. That test class contains so many methods. I need to run the whole test cases in a sequence. How can I set the test case sequence in xUnit?

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

回答by Ruben Bartelink

You can't, by design. It's deliberately random in order to prevent anyone getting one of those either by desire or by accident.

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

The randomness is only for a given Test class, so you may be able to achieve your goals by wrapping items you want to control the order of inside a nested class - but in that case, you'll still end up with random order whenever you have more than two Test Methods in a class.

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

If you're trying to manage the building up of fixtures or context, the built-in IUseFixture<T>mechanism may be appropriate. See the xUnit Cheat Sheetfor examples.

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

But you really need to tell us more about what you're trying to do or we'll just have to get speculative.

但你真的需要告诉我们更多你想要做什么,否则我们只能猜测。

回答by Andreas Reiff

Testpriority: at the bottom of thispage.

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

BTW, I have the same problem right now. And yes, it is not the clean art.. but QA wanted a manual test.. so an automated test with a specific order already is a big leap for them.. (cough) and yes, it is not really unit testing..

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

回答by KnowHoper

In xUnit 2.* this can be achieved using the TestCaseOrdererattribute to designate an ordering strategy, which can be used to reference an attribute that is annotated on each test to denote an order.

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

For example:

例如:

Ordering Strategy

订购策略

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

Attribute

属性

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

    public int Priority { get; private set; }
}

Test Cases

测试用例

[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.* ordering samples here

xUnit 2.*在此订购样品

回答by MarcolinoPT

If you really have the need to prioritize your tests (probably not your unit tests) you can use Xunit.Priority. I have used it for some integration testing and works really well and simple without the overhead of having to write your prioritization classes, for simple case scenarios

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