Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
nunit控制台能否列出测试夹具中的所有测试名称?_Nunit_Unmanaged_Nunit Console - Fatal编程技术网

nunit控制台能否列出测试夹具中的所有测试名称?

nunit控制台能否列出测试夹具中的所有测试名称?,nunit,unmanaged,nunit-console,Nunit,Unmanaged,Nunit Console,我希望在它们运行之前报告它们,并且可以选择通过shell脚本运行单独的测试,而无需管理类别。我们有一些非托管代码,这些代码可能会使进程处于不良状态,有时我们很乐意在每次运行nunit控制台时单独运行每个测试。nunit控制台似乎仍然不支持此选项。然而,使用反射获取测试用例列表是相当简单的。在一个非常基本的级别上,您需要一个具有适当的[Test]/[TestFixture]属性的任何公共类的所有public方法的列表。根据您的测试是如何结构化的,您可能需要进行额外的筛选,例如删除任何标记为代码>

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

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

在基本层面上,代码将如下所示:

// 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);
}
static void PrintTestNames(字符串assemblyName){
Assembly=Assembly.LoadFrom(assemblyName);
foreach(assembly.GetTypes()中的var fixture,其中(x=>x.IsPublic
&&(x.GetCustomAttributes(typeof(TestFixtureAttribute)).Count()>0)
&&(x.GetCustomAttributes(typeof(IgnoreAttribute)).Count()=0))){
foreach(fixture.GetMethods()中的var方法,其中(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))作为IEnumerable;
var casetattributes=method.GetCustomAttributes(typeof(TestCaseAttribute))作为IEnumerable;
var caseSourceAttributes=method.GetCustomAttributes(typeof(TestCaseSourceAttribute))作为IEnumerable;
if(caseAttributes.Count()>0){
foreach(casetattributes中的var testCase){
如果(!string.IsNullOrEmpty(testCase.TestName)){
PrintTestName(fixture.ToString(),testCase.TestName);
}
否则{
字符串参数=提取器参数(testCase.arguments);
PrintTestName(fixture.ToString(),method.Name+参数);
}
}
}
else if(caseSourceAttributes.Count()>0){
foreach(caseSourceAttributes中的var testCase){
var sourceName=testCase.sourceName;
如果(!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成员值;
if(null!=staticMember){
memberValues=staticMember.GetValue(null)为IEnumerable;
}
else if(null!=instanceMember){
var instance=Activator.CreateInstance(fixture);
memberValues=instanceMember.GetValue(实例)为IEnumerable;
}else if(null!=staticMethodMember){
memberValues=staticMethodMember.Invoke(null,新对象[0])作为IEnumerable;
}
else if(null!=instanceMethodMember){
var instance=Activator.CreateInstance(fixture);
memberValues=instanceMethodMember.Invoke(实例,新对象[0])作为IEnumerable;
}
else if(null!=staticPropMember){
memberValues=staticPropMember.GetValue(null)为IEnumerable;
}
else if(null!=instancePropMember){
var instance=Activator.CreateInstance(fixture);
memberValues=instancePropMember.GetValue(实例)为IEnumerable;
}
否则{
WriteLine(“***oops…看起来我不知道如何获取fixture{1}的{0}”,sourceName,fixture.ToString();
继续;
}
foreach(memberValues中的变量memberValue){
if(null!=作为IEnumerable的memberValue){
PrintTestName(fixture.ToString(),method.Name+ExtractArguments(memberValue为
nunit3-console.exe MyProject.dll --explore