繁体   English   中英

如何查找包含nunit测试的DLL文件

[英]How to find dll files containing nunit tests

我有一个包含许多dll的文件夹。 其中一个包含nunit测试(标有[Test]属性的函数)。 我想从c#代码运行nunit测试。 有没有办法找到合适的dll?

谢谢

您可以使用Assembly.LoadFile方法将DLL加载到Assembly对象中。 然后使用Assembly.GetTypes方法获取程序集中定义的所有类型。 然后使用GetCustomAttributes方法,您可以检查类型是否使用[TestFixture]属性进行修饰。 如果你想快速肮脏,你可以在每个属性上调用.GetType()。ToString()并检查字符串是否包含“TestFixtureAttribute”。

您还可以检查每种类型中的方法。 使用方法Type.GetMethods来检索它们,并在每个方法上使用GetCustomAttributes,这次搜索“TestAttribute”。

以防有人需要工作解决方案。 由于无法卸载以这种方式加载的程序集,因此最好将它们加载到另一个AppDomain中。

  public class ProxyDomain : MarshalByRefObject
  {
      public bool IsTestAssembly(string assemblyPath)
      {
         Assembly testDLL = Assembly.LoadFile(assemblyPath);
         foreach (Type type in testDLL.GetTypes())
         {
            if (type.GetCustomAttributes(typeof(NUnit.Framework.TestFixtureAttribute), true).Length > 0)
            {
               return true;
            }
         }
         return false;
      }
   }

     AppDomainSetup ads = new AppDomainSetup();
     ads.PrivateBinPath = Path.GetDirectoryName("C:\\some.dll");
     AppDomain ad2 = AppDomain.CreateDomain("AD2", null, ads);
     ProxyDomain proxy = (ProxyDomain)ad2.CreateInstanceAndUnwrap(typeof(ProxyDomain).Assembly.FullName, typeof(ProxyDomain).FullName);
     bool isTdll = proxy.IsTestAssembly("C:\\some.dll");
     AppDomain.Unload(ad2);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM