【发布时间】:2018-09-03 09:09:31
【问题描述】:
我有一些测试方法分布在多个测试类中,但属于单个测试集合。我正在使用 xUnit 提供的 ITestCaseOrderer,但它仅对单个测试类中的测试方法进行排序。
[AttributeUsage(AttributeTargets.Method)]
public class TestPriorityAttribute : Attribute
{
public TestPriorityAttribute(int priority)
{
this.Priority = priority;
}
public int Priority { get; }
}
我已经按照以下方式实现了我的优先排序器。
public class PriorityOrderer : ITestCaseOrderer
{
public IEnumerable<TTestCase> OrderTestCases<TTestCase>(IEnumerable<TTestCase> testCases) where TTestCase : ITestCase
{
var sortedMethods = new Dictionary<int, TTestCase>();
foreach (var testCase in testCases)
{
var attributeInfo = testCase.TestMethod.Method.GetCustomAttributes(typeof(TestPriorityAttribute).AssemblyQualifiedName)
.SingleOrDefault();
if (attributeInfo != null)
{
var priority = attributeInfo.GetNamedArgument<int>("Priority");
sortedMethods.Add(priority, testCase);
}
}
return sortedMethods.OrderBy(x => x.Key).Select(x => x.Value);
}
}
我的第一个测试类是这样的。
[TestCaseOrderer("Integration.Tests.PriorityOrderer", "CompanyName.ProjectName.Integration.Tests")]
[Collection("StandardIntegrationTests")]
[Trait("Category", "Integration")]
public class StandardControllerTests1
{
public StandardControllerTests1(StandardIntegrationTestFixture standardIntegrationTestFixture)
{
}
[Fact, TestPriority(1)]
public void TestMethod1()
{
}
[Fact, TestPriority(2)]
public void TestMethod2()
{
}
}
我的第二个测试类是这样的
[TestCaseOrderer("Integration.Tests.PriorityOrderer", "CompanyName.ProjectName.Integration.Tests")]
[Collection("StandardIntegrationTests")]
[Trait("Category", "Integration")]
public class StandardControllerTests2
{
public StandardControllerTests2(StandardIntegrationTestFixture standardIntegrationTestFixture)
{
}
[Fact, TestPriority(3)]
public void TestMethod3()
{
}
[Fact, TestPriority(4)]
public void TestMethod4()
{
}
}
我还有其他测试类也属于同一个测试集合。当我运行测试时,它没有在集合中排序。我如何命令这些测试在同一集合中按顺序运行?
【问题讨论】:
-
你能否解释一下为什么你想这样做。这一切听起来都非常错误——我相信你会同意你最终会得到一个更好的解决方案,你不需要这种级别的排序;)
标签: c# unit-testing integration-testing xunit xunit.net