我正在尝试使用NUnitEngine运行测试。但是,它给了我FileNotFoundException:无法加载文件或程序集。如何解决这个问题?

问题描述 投票:1回答:1

我正在从NUnit测试dll文件读取一组测试。我正在使用System.Reflection阅读它。然后,我尝试使用NUnitEngine在该dll中运行测试。

但是,在执行“ runner.Run”时,NUnitEngine引发以下异常FileNotFoundException:无法加载文件或程序集。

我已经检查了路径。它是正确的。谁能告诉我这里的问题是什么?我的代码是用C#编写的。我正在使用.NET Core 3.1

这是我的代码:

using System.IO;
using System.Reflection;
using NUnit.Framework;
using NUnit.Engine;
using System.Xml;

namespace MyNameSpace
{
  public class MyClass
  {

    public void RunTest()
    {
         // set up the options
         string path 
     ="C:/Practice_Code/NUnitTestDemo/bin/Debug/netcoreapp3.1/NUnitTestDemo.dll";

        TestPackage package = new TestPackage(path);

        // prepare the engine
        ITestEngine engine = TestEngineActivator.CreateInstance();
        var _filterService = engine.Services.GetService<ITestFilterService>();
        ITestFilterBuilder builder = _filterService.GetTestFilterBuilder();
        TestFilter emptyFilter = builder.GetFilter();

     using (ITestRunner runner = engine.GetRunner(package))
     {
 // execute the tests            
 XmlNode result = runner.Run(null, emptyFilter); //this line throws the exception
         System.Console.WriteLine("Test Result:");
         System.Console.WriteLine("----------------------------");
         System.Console.WriteLine(result.InnerXml.ToString());
      }
   }//METHOD RunTest ENDS

  }//CLASS MyClass ENDS

}//NAME-SPACE MyNameSpace ENDS

以下是例外详细信息:

NUnit.Engine.NUnitEngineException:'驱动程序在加载测试时发生异常。'

内部异常FileNotFoundException:无法加载文件或程序集'NUnitTestDemo,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null'。系统找不到指定的文件。

c# .net .net-core nunit nunit-console
1个回答
0
投票

我希望您遇到了https://github.com/nunit/nunit-console/issues/710

NUnit Engine的.NET Core构建当前仅在与测试位于同一目录中时有效。解决方法是,尝试将测试程序集及其依赖项复制到与测试运行程序相同的目录中。

这是我们希望为下一版本的NUnit修复的问题。

© www.soinside.com 2019 - 2024. All rights reserved.