1

I have 2 projects in my solution, one is the actual code (NUnitSample1) while the other is the test project (NUnitSample1.Test) with the unit tests. NUnitSample1.Test contains a reference of the first project NUnitSample1. The Build Output path for both projects are different and are explicitly specified. The CopyLocal property of the NUnitSample1 project reference needs to be set to false.

Now, when I build and try to run the unit test using ReSharper, it fails with the following message:

System.IO.FileNotFoundException : Could not load file or assembly 'NUnitSample1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

I guess this is because the binaries are in separate folders. Is there any way to run the tests using ReSharper while maintaining this structure? Also, there are already tens of thousands of tests written, so I need a solution which involves minimal code change. Dynamically loading the assembly (as suggested by AlexeiLevenkov) works, however, it would involve setting up each method individually, which is not feasible.

+NUnitSample1Solution
 +NUnitSample1      //This folder contains the actual class library project
 +NUnitSample1.Test //This folder contains the test project
 +Binaries          //This folder contains the binaries of both projects
  +bin              //This folder contains the project dll
  +tests
   +bin             //This folder contains the test project dll

I found this NUnit link where multiple assemblies may be specified, however, even this does not work when I try to run using ReSharper. I'm also not sure if I'm doing it correctly, where does the config file need to be added? In the actual project or the test project? What is the build action supposed to be?

Any pointers would be appreciated. TIA.

Fahad
  • 1,364
  • 3
  • 20
  • 40
  • Why do you have the `CopyLocal` property set to `false`? – juharr Sep 01 '15 at 14:23
  • If you can't copy assembly you have to load it yourself before using types/calling methods from it. Is your question about "how to use Assembly.LoadXxxx functions"? – Alexei Levenkov Sep 01 '15 at 14:25
  • @juharr Project requirements. Need to adhere to it. – Fahad Sep 01 '15 at 14:28
  • @AlexeiLevenkov I was not aware that I would have to load the assembly myself. I'll look it up. Any pointers would be appreciated. Thanks. – Fahad Sep 01 '15 at 14:30
  • Name of method should be enough for searches - https://www.bing.com/search?q=Assembly.Load ... You can also install assembly in the GAC but I'd really think twice before doing it in development environment (maybe less so it it is integrated into your build). – Alexei Levenkov Sep 01 '15 at 14:36
  • @AlexeiLevenkov I'm able to load the assembly dynamically in a setup method and invoke the method successfully. However, there are tens of thousands of tests and it would require a lot of manual effort to set up each method individually, which is not feasible. Is there any other way this can be done, where I can maybe specify the assembly path in a config file or even in code, but without having to change the existing tests? – Fahad Sep 02 '15 at 10:53
  • Did you try to use this? https://msdn.microsoft.com/en-us/library/system.appdomain.relativesearchpath(v=vs.110).aspx You should be able to set this in code before executing any test cases. – Lex Li Sep 02 '15 at 12:19

1 Answers1

0

I was able to load the missing assembly using the answer from here in the TestFixtureSetup(will also work in the Setup method).

[TestFixtureSetUp]
public void Setup()
{
    AppDomain currentDomain = AppDomain.CurrentDomain;
    currentDomain.AssemblyResolve += new ResolveEventHandler(LoadFromSameFolder);
}

static Assembly LoadFromSameFolder(object sender, ResolveEventArgs args)
{
    string folderPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
    string assemblyPath = Path.Combine(folderPath, new AssemblyName(args.Name).Name + ".dll");           
    if (File.Exists(assemblyPath) == false) return null;
    Assembly assembly = Assembly.LoadFrom(assemblyPath);
    return assembly;
}

The above code in the LoadFromSameFolder method may be modified to locate the assembly exactly.

PS: Thanks to Alexei Levenkov for putting me on the right path.

Community
  • 1
  • 1
Fahad
  • 1,364
  • 3
  • 20
  • 40