English 中文(简体)
以程序方式在尼姑图书馆获得测试清单,无需进行测试
原标题:Get list of tests in nunit library programmatically without having to run tests
  • 时间:2012-05-23 16:52:53
  •  标签:
  • c#
  • nunit

我有一个修女班图书馆,里面有测试案例。我想从程序上获得图书馆中所有测试的清单,主要是测试名称及其测试id。

var runner = new NUnit.Core.RemoteTestRunner();
runner.Load(new NUnit.Core.TestPackage(Request.PhysicalApplicationPath + "bin\SystemTest.dll"));
var tests = new List<NUnit.Core.TestResult>();
foreach (NUnit.Core.TestResult result in runner.TestResult.Results)
{
    tests.Add(result);
}

问题在于运行者。 测试Result 在您实际运行测试之前是无效的。 显然目前我不想运行测试, 我只想获得一份在图书馆中进行测试的清单。 之后, 我将给予用户选择测试并单独运行的能力, 测试代号通过远程测试引擎实例 。

那么,我怎样才能拿到测试清单 而不实际运行所有测试呢?

最佳回答

您可以使用反射来装入组装并查找所有 http://nunit.org/index.php?p=test&r=2.5.10>> 测试 属性。 这将给您所有测试方法。 其余由您决定 。

Here is an example on msdn about using reflection to get the attributes for a type. http://msdn.microsoft.com/en-us/library/z919e8tw.aspx

问题回答

这是从测试类库组装中检索所有测试名称的代码 :

//load assembly.
            var assembly = Assembly.LoadFile(Request.PhysicalApplicationPath + "bin\SystemTest.dll");
            //get testfixture classes in assembly.
            var testTypes = from t in assembly.GetTypes()
                let attributes = t.GetCustomAttributes(typeof(NUnit.Framework.TestFixtureAttribute), true)
                where attributes != null && attributes.Length > 0
                orderby t.Name
                    select t;
            foreach (var type in testTypes)
            {
                //get test method in class.
                var testMethods = from m in type.GetMethods()
                                  let attributes = m.GetCustomAttributes(typeof(NUnit.Framework.TestAttribute), true)
                    where attributes != null && attributes.Length > 0
                    orderby m.Name
                    select m;
                foreach (var method in testMethods)
                {
                    tests.Add(method.Name);
                }
            }

贾斯汀的回答对我没用。以下是(用Test 属性检索所有方法名称 ):

Assembly assembly = Assembly.LoadFrom("pathToDLL");
foreach (Type type in assembly.GetTypes())
{
    foreach (MethodInfo methodInfo in type.GetMethods())
    {
        var attributes = methodInfo.GetCustomAttributes(true);
        foreach (var attr in attributes)
        {
            if (attr.ToString() == "NUnit.Framework.TestAttribute")
            {
               var methodName = methodInfo.Name;
                // Do stuff.
            }
        }
    }
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签