pan*_*o89 6 c# integration-testing unit-testing xunit xunit.net
我有一些测试方法,它们分布在多个测试类中,但属于单个测试集合。我正在使用 xUnit 提供的 ITestCaseOrderer,但它仅对单个测试类中的测试方法进行排序。
[AttributeUsage(AttributeTargets.Method)]
public class TestPriorityAttribute : Attribute
{
public TestPriorityAttribute(int priority)
{
this.Priority = priority;
}
public int Priority { get; }
}
Run Code Online (Sandbox Code Playgroud)
我已经以下面的方式实现了我的优先排序器。
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);
}
}
Run Code Online (Sandbox Code Playgroud)
我的第一个测试类看起来像这样。
[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()
{
}
}
Run Code Online (Sandbox Code Playgroud)
我的第二个测试类看起来像这样
[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()
{
}
}
Run Code Online (Sandbox Code Playgroud)
我还有其他测试类也属于同一个测试集合。当我运行测试时,它不是在整个集合中排序。我如何命令这些测试按同一集合中的顺序运行?
我在构建一个排序系统以与 xUnit 并行运行测试,同时还在自己的集合中运行一些具有依赖项的测试时,也遇到了同样的问题。
根据 xUnit 板上的问题898,目前不可能在同一集合中跨类排序。
我使用的解决方法是使用部分类将需要排序到具有不同文件的同一类中的测试组织起来,以便测试仍然可以组织到不同的文件中。
[Trait("Order", "")]
[TestCaseOrderer(DependencyOrderer.TypeName, DependencyOrderer.AssemblyName)]
public partial class OrderTests
{
[Fact]
public void Test0()
{
Thread.Sleep(TimeSpan.FromSeconds(1));
}
[Fact]
[TestDependency("Test1", "Test0")]
public void Test3()
{
Thread.Sleep(TimeSpan.FromSeconds(1));
}
}
[Trait("Order", "")]
public partial class OrderTests
{
[Fact]
public void Test2()
{
Thread.Sleep(TimeSpan.FromSeconds(1));
}
[Fact]
[TestDependency("Test0")]
public void Test1()
{
Thread.Sleep(TimeSpan.FromSeconds(1));
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3307 次 |
| 最近记录: |