如何在xUnit中设置测试用例序列
我在C#中编写了xUnit测试用例。 该测试类包含很多方法。 我需要按顺序运行整个测试用例。 如何在xUnit中设置测试用例序列?
在xUnit 2. *中,可以使用TestCaseOrderer
属性来指定排序策略,该策略可用于引用在每个测试上注释的属性以表示订单。
例如:
订购策略
public class PriorityOrderer : ITestCaseOrderer { public IEnumerable OrderTestCases (IEnumerable testCases) where TTestCase : ITestCase { var sortedMethods = new SortedDictionary>(); foreach (TTestCase testCase in testCases) { int priority = 0; foreach (IAttributeInfo attr in testCase.TestMethod.Method.GetCustomAttributes((typeof(TestPriorityAttribute).AssemblyQualifiedName))) priority = attr.GetNamedArgument("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(IDictionary 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. * 在这里订购样品
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 } }
顺便说一下,我现在遇到同样的问题。 是的,这不是一个干净的艺术..但QA想要一个手动测试…所以具有特定订单的自动化测试已经是他们的一个重大飞跃……(咳嗽)是的,它不是真正的unit testing..
你不能,按设计。 这是故意随机的,以防止任何人通过欲望或意外获得其中之一。
随机性仅适用于给定的Test类,因此您可以通过包装要控制嵌套类内部顺序的项目来实现目标 – 但在这种情况下,无论何时您仍然会以随机顺序结束在一个类中有两个以上的测试方法。
如果您正在尝试管理灯具或上下文的构建,则内置的IUseFixture
机制可能是合适的。 有关示例,请参阅xUnit备忘单 。
但你真的需要告诉我们更多关于你要做什么的事情,或者我们只需要投机。