5

我想在它们运行之前报告它们,并且可以选择通过 shell 脚本运行单个测试而不管理类别。我们有一些非托管代码可能会使进程处于不良状态,有时很乐意在每次 nunit-console 运行时单独运行每个测试。

4

2 回答 2

3

现在有一个--explore命令行选项可用于在不运行测试的情况下列出所有测试用例。进一步来说

nunit3-console.exe MyProject.dll --explore

欲了解更多信息:https ://github.com/nunit/docs/wiki/Console-Command-Line#description

于 2017-11-07T20:59:58.050 回答
0

nunit-console 似乎仍然不支持此选项。然而,使用反射获取测试用例列表相当简单。在非常基本的级别上,您需要具有适当属性public的 any 的所有方法的列表。根据测试的结构,您可能需要进行额外的过滤,例如删除任何标记有属性的测试或考虑基类中的测试方法。public class[Test]/[TestFixture][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 属性,例如TestCase和,这会变得有点复杂TestCaseSource。我修改了下面的代码以支持这些属性的一些功能。

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);
}

如果您查看上面的代码,您可能会注意到我已经处理了TestCaseSourcefor 测试是一个命名属性/方法/字段的字符串的功能。您还会注意到,虽然有更多代码,但它仍然是非常简单的代码,因此如果您使用 TestCaseSource 的替代版本,或者如果您正在使用我没有的其他 NUnit 属性,则可以轻松扩展它t 迎合。

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

于 2015-05-19T11:56:50.080 回答