Get total count of tests in NUnit3

Viewed 1167

I would like to get the total number of tests that NUnit3 is planning on running, for calculating progress, time remaining etc.

I would like to get this information from within a test, so that I can print information to the console about how many tests are remaining.

I have looked around the standard interface NUnit.Framework.TestContext but I can't spot anything that gives a list of the tests.

Some googling pointed me to the NUnit.Engine API but that seems to be for external use - every constructor wants the path to the test assembly and I can't even figure out how to get that information out of the TestContext.

Can anyone point me in the right direction?

1 Answers
    // 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;
}
Related