2012-02-09 65 views
12

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

+5

为什么您关心测试执行的顺序?测试以任何方式相互依赖通常是一个坏主意 - 它们应该是彼此独立的。 – 2012-02-09 11:47:12

+7

虽然对于集成测试而言,单元测试确实如此。 – 2016-03-14 15:25:32

回答

0

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

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

如果您试图管理固定装置或上下文的建立,则内置的IUseFixture<T>机制可能是适当的。示例请参见xUnit Cheat Sheet

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

14

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想要一个手动测试..所以具有特定顺序的自动化测试已经是他们的一大飞跃..(咳嗽),是的,它不是真正的单元测试..

+0

准确的答案我会给。 ;) – bricelam 2013-01-22 16:23:19

+0

其实,现在我看到你的评论..我记得。 :) TestPriority仅适用于每个模块/类的基础上。所以不同的类仍然以随机顺序执行。 (C#编译器将类以不可预测的顺序嵌入到程序集中。)为了使测试序列(测试程序具有测试协议)更加简单和可重复,我添加了按字母顺序排序。所以我创建了xunit 1.9的修改版本,它按字母顺序执行测试类。看看http://www.andreas-reiff.de/2012/06/xunit-with-alphabetically-sorted-classes-and-proper-display-list-matching-execution-order/。 – 2013-01-22 21:13:05

+0

@bricelam,那个链接似乎坏了,它进入了一些重定向循环。 – 2015-12-23 19:36:20

12

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

例如:

订货策略

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