Smocks icon indicating copy to clipboard operation
Smocks copied to clipboard

Base Directory resolving issue when using NUnit

Open harigutta opened this issue 8 years ago • 3 comments

We package and run all of our test assemblies at once using NUnit config like below

<NUnitProject>
  <Settings activeconfig="Default" appbase="PathToTestAssemblies" processModel="Default" domainUsage="Default" />
  <Config name="Default" binpathtype="Auto" runtimeFramework="net-4.0">
    <assembly path="PathRelativeToAppBase\Test1.exe" />
    <assembly path="PathRelativeToAppBase\Test2.exe" />
  </Config>
</NUnitProject>

With the above setup, when the NUnit initializes the AppDomain, its Directory is "PathToTestAssemblies" and not the actual assembly it is executing causing dll not found exception in the AssemblyLoader class.

I had to switch up the RunAction and RunFunc methods like below to resolve and pass the correct BaseDirectory

var basePath = action.Method.DeclaringType != null
                ? Path.GetDirectoryName(action.Method.DeclaringType.Assembly.Location)
                : AppDomain.CurrentDomain.BaseDirectory;

            using (AppDomainContext context = new AppDomainContext(new AssemblyLoaderFactory(basePath, rewriter),
                new Serializer(), configuration.Logger))
            {
                configuration.Logger.Info("Creating service locator for app domain {0}", context);
                context.Invoke(new Action(() => ServiceLocator.Instance = CreateServiceLocator(configuration)));

                configuration.Logger.Info("Invoking action in app domain {0}", context);
                context.Invoke(action, _serviceLocator.Resolve<ISmocksContext>());
            }

harigutta avatar Mar 15 '16 14:03 harigutta

Is the base directory for the AppDomain literally the string "PathToTestAssemblies"? That is, not a valid path to an existing directory?

vanderkleij avatar Mar 29 '16 08:03 vanderkleij

Oh, "PathToTestAssemblies" is a valid path on my machine.

For eg.

Appbase = "C:\harigutta\MyProject\Testing\UnitTests\bin\Release"

AssemblyPath1 = "C:\harigutta\MyProject\Testing\UnitTests\bin\Release\UnitTests\TestProject1\TestProject1.exe"
AssemblyPath2 = "C:\harigutta\MyProject\Testing\UnitTests\bin\Release\UnitTests\TestProject2\TestProject2.exe"

NUnit Test Runner loads the dlls from Appbase and continues to load from the different AssemblyPaths.

harigutta avatar Mar 29 '16 17:03 harigutta

Thanks for the clarification. I'll add functionality to handle this case.

vanderkleij avatar Mar 30 '16 12:03 vanderkleij