我已經用C#編寫了xUnit測試用例。那個測試類包含了很多方法。我需要按順序運行整個測試用例。我如何在xUnit中設置測試用例序列?如何在xUnit中設置測試用例序列
回答
你不能通過設計。它是故意隨機的,以防止任何人通過願望或意外獲得其中一個。
隨機性僅適用於給定的Test類,因此您可以通過包裝要控制嵌套類內部順序的項目來實現目標 - 但在這種情況下,您仍然會以隨機順序,只要你在一個類中有兩個以上的測試方法。
如果您試圖管理固定裝置或上下文的建立,則內置的IUseFixture<T>
機制可能是適當的。示例請參見xUnit Cheat Sheet。
但是你真的需要告訴我們更多關於你想要做什麼或者我們只需要投機。
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想要一個手動測試..所以具有特定順序的自動化測試已經是他們的一大飛躍..(咳嗽),是的,它不是真正的單元測試..
準確的答案我會給。 ;) – bricelam 2013-01-22 16:23:19
其實,現在我看到你的評論..我記得。 :) 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
@bricelam,那個鏈接似乎壞了,它進入了一些重定向循環。 – 2015-12-23 19:36:20
在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
- 1. 如何設置XUnit測試的結果
- 2. 如何在dotnetcore xunit測試中設置文化
- 3. Matlab xUnit Framework測試套件設置
- 4. 在Hudson中配置xUnit測試輸出
- 5. 使用xUnit測試運行NUnit測試用例
- 6. 如何使用XUnit編寫簡單的測試用例?
- 7. JUnit設置測試用例
- 8. 如何使用xUnit測試void方法?
- 9. 如何在xUnit中記錄測試的執行順序
- 10. 如何設置Apache httpd測試實例?
- 11. 如何在django測試用例中設置cookie?
- 12. 如何在junit測試用例中設置env變量?
- 13. 在Visual C#Express中調試xUnit測試?
- 14. TestNG測試用例序列
- 15. 如何在運行時使用測試ng來設置測試用例名稱?
- 16. 如何使用xunit配置測試流程
- 17. 如何在xunit測試中調試理論
- 18. 如何使用XUnit工具在asp.net核心編寫功能測試用例?
- 19. 如何在Visual Studio 2012 Express中啓用xUnit測試?
- 20. 無法找到任何匹配的測試用例的xUnit
- 21. XUnit測試DbContext沒有配置
- 22. Xunit在一個應用程序域中運行所有測試
- 23. 的xUnit測試在.net中的Core 2
- 24. 如何設置sentestkit測試順序?
- 25. 如何在jenkins中觸發之前設置/配置測試用例
- 26. JUnit測試用例 - 設置數據
- 27. 如何設計測試用例
- 28. 與Xunit測試asp.net核心2.0項目如何測試httpget
- 29. 測試設置例程設計模式
- 30. 在VS2015上用xUnit測試UWP應用程序
爲什麼您關心測試執行的順序?測試以任何方式相互依賴通常是一個壞主意 - 它們應該是彼此獨立的。 – 2012-02-09 11:47:12
雖然對於集成測試而言,單元測試確實如此。 – 2016-03-14 15:25:32