【问题标题】:Get list of tests in nunit library programmatically without having to run tests以编程方式获取 nunit 库中的测试列表,而无需运行测试
【发布时间】:2017-05-02 16:09:04
【问题描述】:

我有一个包含测试用例的 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);
}

问题是 runner.TestResult 在您实际运行测试之前为空。我显然不想在这一点上运行测试,我只想获取库中哪些测试的列表。之后,我将让用户能够选择一个测试并单独运行它,将测试 id 传递给 RemoteTestRunner 实例。

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

【问题讨论】:

    标签: c# nunit


    【解决方案1】:

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

    这是 msdn 上有关使用反射获取类型属性的示例。 http://msdn.microsoft.com/en-us/library/z919e8tw.aspx

    【讨论】:

    • +1,但是有一个转折点:使用 TestCaseAttribute,您可以对测试方法进行参数化,从而将其转换为多个(逻辑)测试。没有什么是不能通过反思来处理的,但要记住一些事情。
    • 我最初正在考虑这样做,因为它会给我测试(函数)名称,但它不会给我测试 ID。只要我可以通过测试名称而不是测试 ID 过滤 RemoteTestRunner 运行,那么这应该可以正常工作,会检查出来。
    • 不幸的是,我似乎无法通过测试名称过滤 RemoteTestRunner,我也需要测试 id,所以我无法将其标记为正确答案。
    • @Justin ID 总是一样的吗?我对 MSTest 比对 nUnit 更熟悉。
    • 这听起来像是在测试运行期间生成的 id 可以回答您的问题......或者不幸的是使其无效。
    【解决方案2】:

    这是从测试类库程序集中检索所有测试名称的代码:

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

    【讨论】:

      【解决方案3】:

      贾斯汀的回答对我不起作用。以下是(检索所有具有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.
                  }
              }
          }
      }
      

      【讨论】:

      • 这行得通。如果您想获取当前正在运行的 testclass 中的测试计数而不是整个 dll,请添加以下代码: if (TestContext.CurrentContext.Test.ClassName.Contains(type.Name)) {...
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-04
      • 2011-05-04
      • 1970-01-01
      • 2014-12-12
      • 2021-08-08
      • 1970-01-01
      相关资源
      最近更新 更多