C# 以编程方式获取nunit库中的测试列表,而无需运行测试

C# 以编程方式获取nunit库中的测试列表,而无需运行测试,c#,nunit,C#,Nunit,我有一个包含测试用例的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.Te

我有一个包含测试用例的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);
}
var runner=new NUnit.Core.RemoteTestRunner();
Load(新的NUnit.Core.TestPackage(Request.PhysicalApplicationPath+“bin\\SystemTest.dll”);
var测试=新列表();
foreach(runner.TestResult.Results中的NUnit.Core.TestResult)
{
测试。添加(结果);
}
问题在于,在实际运行测试之前,runner.TestResult为null。显然,我现在不想运行测试,我只想得到库中哪些测试的列表。之后,我将为用户提供选择测试并单独运行它的能力,并将测试id传递给RemoteTestRunner实例


那么,如何才能在不实际运行所有测试的情况下获得测试列表呢?

您可以使用反射加载程序集并查找所有属性。这将为您提供所有测试方法。剩下的就看你了

下面是msdn上的一个示例,介绍如何使用反射来获取类型的属性。
以下是从测试类库程序集中检索所有测试名称的代码:

//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
属性的所有方法名):


+1,但是有一个转折点:使用TestCaseAttribute可以参数化测试方法,从而将其转换为多个(逻辑)测试。没有什么是反射无法处理的,但要记住的。我最初考虑这样做,因为它会给我测试(函数)名称,但它不会给我测试ID。只要我能用测试名而不是测试id来过滤RemoteTestRunner运行,那么这应该行得通,我会检查它。不幸的是,我似乎不能用测试名过滤RemoteTestRunner,我也需要测试id,所以我不能将其标记为正确答案。@Justin id是否总是一样的?我更熟悉MSTest而不是nUnit。这让它听起来像是在测试运行期间生成的ID,它回答了您的问题。。。或者不幸地使它无效。这是有效的。如果要获取当前运行的testclass isstead中整个dll的测试计数,请添加以下代码:If(TestContext.CurrentContext.Test.ClassName.Contains(type.Name)){。。。
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.
            }
        }
    }
}