获取NUnit3中的测试总数

时间:2018-09-12 22:46:08

标签: nunit-3.0

我想获得NUnit3计划运行的测试总数,以计算进度,剩余时间等。

我想从测试中获取此信息,以便可以向控制台打印有关剩余多少测试的信息。

我已经看过标准界面NUnit.Framework.TestContext,但是我找不到能提供测试列表的任何东西。

有些谷歌搜索使我指向NUnit.Engine API,但这似乎是供外部使用的-每个构造函数都需要测试组件的路径,而我什至不知道如何获得那个 TestContext中的信息。

有人能指出我正确的方向吗?

1 个答案:

答案 0 :(得分:0)

    // Using reflection to get total number of tests in a class

using System;
using System.Linq;
using System.Reflection;
using NUnit;

int GetTotalTestCount(Type testClassType)
{
    int result = 0;

    foreach (MethodBase method in testClassType.GetMethods())
    {
        if (method.GetCustomAttributes<TestAttribute>().Any())
        {
            result++;
        }
    }

    return result;
}