有没有办法获取xunit中的测试总数?

问题描述 投票:0回答:1

是否有办法获取 xunit 中的测试总数?

我知道我可以运行测试并以这种方式获取值,但我只想运行一个命令来获取框架中的测试总量以用于报告目的,而无需运行完整套件来获取值。 Xunit。 C#

c# testing count xunit
1个回答
0
投票

你可以使用反射。

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());
© www.soinside.com 2019 - 2024. All rights reserved.