是否有办法获取 xunit 中的测试总数?
我知道我可以运行测试并以这种方式获取值,但我只想运行一个命令来获取框架中的测试总量以用于报告目的,而无需运行完整套件来获取值。 Xunit。 C#
你可以使用反射。
xunit
中的每个测试都标记为 [fact]
或 [theory]
// You should load the assembly containing your tests. Replace "YourTestAssembly.dll" with the path to your actual test assembly.
var testAssembly = Assembly.LoadFrom("YourTestAssembly.dll");
// Get all types from the assembly
var testTypes = testAssembly.GetTypes();
// Count all methods with Fact or Theory attributes
var testMethodsCount = testTypes.SelectMany(type => type.GetMethods())
.Count(method => method.GetCustomAttributes<FactAttribute>().Any()
|| method.GetCustomAttributes<TheoryAttribute>().Any());