我已经用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