2012-02-09 72 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