nunit控制台可以列出测试夹具中的所有测试名称



我想在它们运行之前报告它们,并可以选择在不管理类别的情况下通过shell脚本运行单个测试。我们有一些非托管代码,这些代码可能会使进程处于糟糕的状态,有时我们很乐意在每次nunit控制台运行时单独运行每个测试。

现在有了--explore命令行选项,可以在不运行测试的情况下列出所有测试用例。更具体地说
nunit3-console.exe MyProject.dll --explore

有关更多信息:https://github.com/nunit/docs/wiki/Console-Command-Line#description

nunit控制台似乎仍然不支持此选项。然而,使用反射来获得测试用例列表是相当简单的。在非常基本的级别上,您需要一个具有适当[Test]/[TestFixture]属性的任何public class的所有public方法的列表。根据测试的结构,您可能需要进行额外的筛选,例如删除任何用[Ignore]属性标记的测试,或者考虑基类中的测试方法。

在基本级别上,代码看起来像这样:

// Load the assembly containing your fixtures
Assembly a = Assembly.LoadFrom(assemblyName);
// Foreach public class that is a TestFixture and not Ignored
foreach (var c in a.GetTypes()
                   .Where(x=>x.IsPublic 
                   && (x.GetCustomAttributes(typeof(NUnit.Framework.TestFixtureAttribute)).Count() > 0)
                   && (x.GetCustomAttributes(typeof(NUnit.Framework.IgnoreAttribute)).Count() ==0))) 
{
    // For each public method that is a Test and not Ignored
    foreach (var m in c.GetMethods()
                       .Where(x=>x.IsPublic
                       && (x.GetCustomAttributes(typeof(NUnit.Framework.TestAttribute)).Count() > 0)
                       && (x.GetCustomAttributes(typeof(NUnit.Framework.IgnoreAttribute)).Count() ==0))) 
    {
        // Print out the test name
        Console.WriteLine("{0}.{1}", c.ToString(), m.Name);
        // Alternately, print out the command line to run test case using nunit-console
        //Console.WriteLine("nunit-console /run:{0}.{1} {2}", c.ToString(), m.Name, assemblyName);
    }
}

显然,如果您只想要特定TestFixture的测试方法,那么您可以稍微简化一下。

正如在评论中所说,如果您需要注意其他NUnit属性,如TestCaseTestCaseSource,这会变得有点复杂。我修改了下面的代码以支持这些属性的一些功能。

static void PrintTestNames(string assemblyName) {
    Assembly assembly = Assembly.LoadFrom(assemblyName);
    foreach (var fixture in assembly.GetTypes().Where(x => x.IsPublic
                                      && (x.GetCustomAttributes(typeof(TestFixtureAttribute)).Count() > 0)
                                      && (x.GetCustomAttributes(typeof(IgnoreAttribute)).Count() == 0))) {
        foreach(var method in fixture.GetMethods().Where(x=>x.IsPublic
            && (x.GetCustomAttributes(typeof(IgnoreAttribute)).Count() == 0)
            && ((x.GetCustomAttributes(typeof(TestAttribute)).Count() > 0)
                || (x.GetCustomAttributes(typeof(TestCaseAttribute)).Count() > 0)
                || (x.GetCustomAttributes(typeof(TestCaseSourceAttribute)).Count() > 0))
            )) {
            var testAttributes = method.GetCustomAttributes(typeof(TestAttribute)) as IEnumerable<TestAttribute>;
            var caseAttributes = method.GetCustomAttributes(typeof(TestCaseAttribute)) as IEnumerable<TestCaseAttribute>;
            var caseSourceAttributes = method.GetCustomAttributes(typeof(TestCaseSourceAttribute)) as IEnumerable<TestCaseSourceAttribute>;
            if (caseAttributes.Count() > 0) {
                foreach(var testCase in caseAttributes) {
                    if (!string.IsNullOrEmpty(testCase.TestName)) {
                        PrintTestName(fixture.ToString(), testCase.TestName);
                    }
                    else {
                        string arguments = ExtractArguments(testCase.Arguments);
                        PrintTestName(fixture.ToString(), method.Name + arguments);
                    }
                }
            }
            else if (caseSourceAttributes.Count() > 0) {
                foreach (var testCase in caseSourceAttributes) {
                    var sourceName = testCase.SourceName;
                    if (!string.IsNullOrEmpty(sourceName)) {
                        var staticMember = fixture.GetField(sourceName, BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
                        var instanceMember = fixture.GetField(sourceName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
                        var staticMethodMember = fixture.GetMethod(sourceName, BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
                        var instanceMethodMember = fixture.GetMethod(sourceName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
                        var staticPropMember = fixture.GetProperty(sourceName, BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
                        var instancePropMember = fixture.GetProperty(sourceName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);

                        IEnumerable memberValues;
                        if (null != staticMember) {
                            memberValues = staticMember.GetValue(null) as IEnumerable;
                        }
                        else if (null != instanceMember) {
                            var instance = Activator.CreateInstance(fixture);
                            memberValues = instanceMember.GetValue(instance) as IEnumerable;
                        } else if(null != staticMethodMember) {
                            memberValues = staticMethodMember.Invoke(null,new object [0]) as IEnumerable;
                        }
                        else if (null != instanceMethodMember) {
                            var instance = Activator.CreateInstance(fixture);
                            memberValues = instanceMethodMember.Invoke(instance, new object[0]) as IEnumerable;
                        }
                        else if (null != staticPropMember) {
                            memberValues = staticPropMember.GetValue(null) as IEnumerable;
                        }
                        else if (null != instancePropMember) {
                            var instance = Activator.CreateInstance(fixture);
                            memberValues = instancePropMember.GetValue(instance) as IEnumerable;
                        }
                        else {
                            Console.WriteLine("*** Ooops...Looks like I don't know how to get {0} for fixture {1}", sourceName, fixture.ToString());
                            continue;
                        }
                        foreach (var memberValue in memberValues) {
                            if (null != memberValue as IEnumerable) {
                                PrintTestName(fixture.ToString(), method.Name + ExtractArguments(memberValue as IEnumerable));
                            }
                            else {
                                PrintTestName(fixture.ToString(), method.Name + "(" + memberValue.ToString() + ")");
                            }
                        }
                    } else {
                        Console.WriteLine("*** Ooops...Looks like I don't know how to handle test {0} for fixture {1}", method.Name, fixture.ToString());
                    }
                }
            }
            else {
                PrintTestName(fixture.ToString(), method.Name);
            }
        }
    }
}
static string ExtractArguments(IEnumerable arguments) {
    string caseArgs = "(";
    bool first = true;
    foreach (var arg in arguments) {
        if (first) first = false;
        else caseArgs += ",";
        caseArgs += Convert.ToString(arg);
    }
    return caseArgs + ")";
}
static void PrintTestName(string fixture, string testName) {
    Console.WriteLine("{0}.{1}", fixture, testName);
    //Console.WriteLine("nunit-console /run:{0}.{1} {2}", fixture, testName, assemblyName);
}

如果您仔细阅读上面的代码,您可能会注意到我已经处理了一些功能,其中用于测试的TestCaseSource是一个命名属性/方法/字段的字符串。您还会注意到,虽然还有更多的代码,但它仍然是非常简单的代码,因此如果您使用TestCaseSource的替代版本,或者如果您使用的其他NUnit属性我没有考虑到,它可以很容易地扩展。

在上面添加一个计数器也很容易,这样你就可以放心地打印出与nunit控制台将运行的测试数量相同的测试数量。

最新更新