以编程方式从ASP.NET Core应用程序运行.NET Core测试

时间:2019-06-20 12:57:49

标签: c# asp.net-core nunit

当我尝试以编程方式从ASP.NET Core应用程序运行.Net Core nunit测试时,出现一些异常。

我正在使用:

  • .Net Core Version =“ 2.1”
  • NUnit.Engine版本=“ 3.10.0”

我的代码如下:

var assemblyPath = @"C:\path\to\test.dll"
ITestEngine engine = TestEngineActivator.CreateInstance();
TestPackage package = new TestPackage(assemblyPath);
ITestRunner runner = engine.GetRunner(package)
XmlNode testResult = runner.Run(this, TestFilter.Empty);

test.dll-只有一个带有测试的项目,该项目类型为ClassLibrary。

运行此代码时,我遇到以下异常。

NUnit.Engine.NUnitEngineException: 'An exception occurred in the driver 
while loading tests.' 
FileNotFoundException: Could not load file or assembly 'test.dll, 
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. The system cannot 
find the file specified.

如何以编程方式运行.net核心测试?

1 个答案:

答案 0 :(得分:0)

该错误表明找不到该文件,请检查路径并进行调试,以查看文件是否已在new TestPackage(assemblyPath)行加载

即使您也可以尝试按以下方式加载程序集

var myAssembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(assemblyPath);