2

I am reading a set of tests from an NUnit test dll file. I am reading it using System.Reflection. Then I am trying to run a test inside that dll using NUnitEngine.

But, while doing "runner.Run", NUnitEngine throws the following exception FileNotFoundException: Could not load file or assembly.

I have checked the path. It is correct. Can anybody please tell what could be the issue here ? My code is written in C#. I am using .NET Core 3.1

This is my code:

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

Here is the exception details:

NUnit.Engine.NUnitEngineException: 'An exception occurred in the driver while loading tests.'

Inner Exception FileNotFoundException: Could not load file or assembly 'NUnitTestDemo, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.

Ayan
  • 115
  • 2
  • 10

1 Answers1

1

I expect you're encountering https://github.com/nunit/nunit-console/issues/710.

The .NET Core build of the NUnit Engine currently only works when it is located in the same directory as the tests. As a workaround, try copying your test assembly and it's dependencies to the same directory as your test runner.

This is something we're hoping to fix for the next version of NUnit.

Addendum: fixed from 3.12.0-beta1 (https://www.nuget.org/packages/NUnit.Engine/3.12.0-beta1)

James Wiseman
  • 29,946
  • 17
  • 95
  • 158
Chris
  • 5,882
  • 2
  • 32
  • 57
  • I copied the test dll and its dependencies to the same directory as my test runner. But, still it is throwing the same exception. – Ayan May 28 '20 at 06:13
  • How are you running your .NET Core app - have you published it? You'll need to do that too. – Chris May 28 '20 at 19:51
  • 1
    I stubled across this with v3.11 of Nuget.Engine. Took the pre-release of 3.12 and it fixed this. Thanks @Chris and all for resolving – James Wiseman Aug 25 '20 at 19:33
  • 1
    Thanks for the edit @JamesWiseman - glad you're up and running! – Chris Aug 26 '20 at 21:48