我已经用 C# 编写了 xUnit 测试用例。该测试类包含很多方法。我需要按顺序运行整个测试用例。如何在 xUnit 中设置测试用例序列?
5 回答
在 xUnit 2.* 中,这可以使用TestCaseOrderer
属性来指定排序策略来实现,该策略可用于引用在每个测试上注释的属性以表示订单。
例如:
订购策略
[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;
}
}
属性
[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.*在此处订购样品
测试优先级:在本页底部。
[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 想要手动测试..所以具有特定顺序的自动化测试对他们来说已经是一个很大的飞跃..(咳嗽)是的,这不是真正的单元测试..
如果你真的需要优先考虑你的测试(可能不是你的单元测试),你可以使用Xunit.Priority。我已经将它用于一些集成测试,并且工作得非常好和简单,而无需编写优先级类的开销,用于简单的案例场景
出于某种原因,XUnit.Priority
对我不起作用。在我的测试用例中,它没有按照指定的优先级顺序运行测试。
所以我尝试XUnitPriorityOrderer
了,它类似于使用但正在工作(为了快速测试它,将以下代码在文本编辑器中保存为 OrderedXUnitTests.linq,然后使用LinqPad 6打开并执行它。或者,您也可以将 TestClass 复制到Visual Studio 并添加 XUnit、XUnit.Runner.VisualStudio 和 XUnitPriorityOrderer):
<Query Kind="Program">
<NuGetReference>XUnitPriorityOrderer</NuGetReference>
<Namespace>Xunit</Namespace>
<Namespace>XUnitPriorityOrderer</Namespace>
</Query>
#load "xunit"
// using XUnitPriorityOrderer
// see: https://github.com/frederic-prusse/XUnitPriorityOrderer
void Main()
{
RunTests(); // Call RunTests() or press Alt+Shift+T to initiate testing.
}
#region private::Tests
[TestCaseOrderer(CasePriorityOrderer.TypeName, CasePriorityOrderer.AssembyName)]
public class TestClass
{
static List<string> Order { get; set; }
public TestClass()
{
Order = Order ?? new List<string>();
}
[Fact, Order(2)]
void Test_Xunit_AnotherTest()
{
Order.Add("Test_Xunit_AnotherTest");
Assert.True(3 + 1 == 4);
}
[Fact, Order(1)]
void Test_Xunit()
{
Order.Add("Test_XUnit");
Assert.True(1 + 1 == 2);
}
[Fact, Order(99)]
void Print_Order()
{
Order.Add("Print_Order");
var strOrder = string.Join(", ", Order.ToArray());
strOrder.Dump("Execution Order");
Assert.True(true);
}
}
#endregion
这将按照给定的顺序(Order(1)、Order(2) 和 Order(99))运行测试,并最终转储执行的测试(测试方法 Print_Order())。
你不能,按照设计。这是故意随机的,以防止任何人出于愿望或意外获得其中之一。
随机性仅适用于给定的 Test 类,因此您可以通过包装要控制嵌套类内部顺序的项目来实现目标 - 但在这种情况下,无论何时您仍然会以随机顺序结束一个类中有两个以上的测试方法。
如果您正在尝试管理固定装置或上下文的构建,则内置IUseFixture<T>
机制可能是合适的。有关示例,请参阅xUnit 备忘单。
但是你真的需要告诉我们更多关于你想要做什么的信息,否则我们将不得不投机。