48

When I run unit tests with Visual Studio it works fine, because it runs from project directory where all assemblies are. But when I run it with resharper it goes with error on

var services = Assembly.Load("SomeAssembly");

with error

Could not load file or assembly 'SomeAssembly' or one of its dependencies. The system cannot find the file specified..

So i've tried

var path = Assembly.GetExecutingAssembly().Location;

and it's not project one. It's

C:\Users\*UserName*\AppData\Local\Temp\TestResults\...\Out\

and there is no 'SomeAssembly'. How do I configure resharper correctly or collect all assemblies like Visual Studio does?

It happens with unit tests but not with NUnit, any ideas?

MikeMurko
  • 2,214
  • 1
  • 27
  • 56
Roar
  • 2,117
  • 4
  • 24
  • 39

10 Answers10

44

Resharper shadow copies assemblies for testing by default. If you turn off shadow-copy, it will run in the bin folder and the test should pass. Here are some instructions on turning it off.

tallseth
  • 3,635
  • 1
  • 23
  • 24
  • it happens with unit tests but not with nunit, so what's the different, any ideas? @tallseth – Roar Apr 30 '13 at 06:24
  • What do you mean when you say "unit tests, but not with nunit"? Do you mean MSTest? Or do you mean it works with the nunit console runner, but not with the resharper plugin, and all the tests use the nunit framework? – tallseth May 01 '13 at 04:14
  • 1
    yeah, when i run MSTest with resharper it fails, but when i run MSTest with TestExplorer it runs ok and when i run NUnit with resharper it runs well t00 – Roar May 01 '13 at 06:55
  • I've disabled shadow-copy, but still have the same issue. It works if I run it from Test Explorer, but not via Resharper. – Remy May 27 '13 at 12:29
  • So if it is an nunit test, it runs fine in all cases, and mstests do not? Stop using mstext, switch to nunit. Here's a guide: http://knot.org/scottlaw/blog/?p=404 – tallseth May 28 '13 at 13:08
  • Another idea could be to try and update R#, as had a similar issue, whereas a `shadow-copy` wasn't solving it. Was a version bug. – Alex M Apr 04 '18 at 13:49
26

In the documentation for NUnit's Gui Test Runner settings has the following note about Shadow Copy

Note: If you are tempted to disable shadow copy in order to access files in the same directory as your assembly, you should be aware that there are alternatives. Consider using the Assembly.Codebase property rather than Assembly.Location.

Here is an example of using the Assembly.Codebase property

    private string AssemblyLocation()
    {
        var assembly = Assembly.GetExecutingAssembly();
        var codebase = new Uri(assembly.CodeBase);
        var path = codebase.LocalPath;
        return path;
    }
mcdon
  • 4,931
  • 3
  • 38
  • 36
  • 9
    Came accross [TestContext.CurrentContext.TestDirectory](https://github.com/nunit/docs/wiki/TestContext#testdirectory). I'm using it now in NUnit 3.0. I don't know how far back TestContext.CurrentContext.TestDirectory was available. – mcdon Jul 05 '16 at 18:47
  • You should add the TestContext solution as a different answer, as it is an excellent answer in its own right. I'd vote for it. This did work in NUnit 2.6 also, and I think 2.x versions in general. – Holf Dec 09 '16 at 10:44
12

I had the same problem, resharper test runner was in C:\ whereas the actual built dlls and solution were on another drive. The solution was to untick "Use Legacy Runner" in the MSTest settings page in resharper options.

sinaptik
  • 331
  • 5
  • 15
3

Try to create a testsettings file, and configure deployment rules for your tests.

Older versions of resharper seem to have some bugs when processing deployment of folders, I think it is fixed in latest version of resharper 7.

Community
  • 1
  • 1
Alexander
  • 4,153
  • 1
  • 24
  • 37
3

Try this code for loading (see below). It will lookup assemblies independent of test runner.

private static string[] assemblyLookupPath = new[]
{
    AppDomain.CurrentDomain.BaseDirectory, 
    Environment.CurrentDirectory,
    Assembly.GetExecutingAssembly().Location
}.Distinct().ToArray();

public static void Assembly Load(string fileName)
{
     var filePath = assemblyLookupPath
         .Select(f=>Path.Combine(f, fileName))
         .Where(File.Exists)
         .FirstOrDefault();

     /*do here null checks and raise errors, write logs, etc*/

     return Assembly.LoadFrom(filePath )
}
Julien Roncaglia
  • 17,397
  • 4
  • 57
  • 75
Manushin Igor
  • 3,398
  • 1
  • 26
  • 40
2

You are loading your assemblies dynamically by using Assembly.Load(). May be you are missing a reference to the assembly to load. Otherwise shadow-copying may miss the unreferenced assemblies.

If you do not want to reference these assemblies, be sure to include them in your project and copy them to the output directory. You can do so by setting the "Copy to Output Directory" property or creating a custom post-build step.

Dio F
  • 2,458
  • 1
  • 22
  • 39
  • I reference needed assembly, it works fine if NUnit test, but not work with default Visual Studio test Microsoft.VisualStudio.TestTools.UnitTesting; – Roar Apr 30 '13 at 09:29
  • No idea so far. BTW: The Microsoft.VisualStudio.TestTools.UnitTesting functionality is often referred to as "MSTest". Maybe this helps getting better search results. – Dio F Apr 30 '13 at 10:44
2

Just to complete the very helpful answer from mcdon, using assembly.Location gives the correct answer as per MSFT's explanation:

The CodeBase is a URL to the place where the file was found, while the Location is the path from where it was actually loaded. For example, if the assembly was downloaded from the internet, its CodeBase may start with “http://”, but its Location may start with “C:\”. If the file was shadow copied, the Location would be the path to the copy of the file in the shadow-copy dir.

It’s also good to know that the CodeBase is not guaranteed to be set for assemblies in the GAC. Location will always be set for assemblies loaded from disk, however.

Therefore I would use the following:

public static DirectoryInfo GetAssemblyDirectory()
{
    var assembly = Assembly.GetExecutingAssembly();    
    return new DirectoryInfo(Path.GetDirectoryName(assembly.Location));
}
MaYaN
  • 6,683
  • 12
  • 57
  • 109
1

Just change the current directory

var dir = Path.GetDirectoryName(typeof(MySetUpClass).Assembly.Location);
Environment.CurrentDirectory = dir;

// or
Directory.SetCurrentDirectory(dir);

https://github.com/nunit/nunit/issues/1072

RouR
  • 6,136
  • 3
  • 34
  • 25
0

What solved it for me was to set the "Copy Local" property to true on the nunit.framework.dll reference in the test project.

HS1
  • 3
  • 1
  • 2
0

If you have run and build problem after disabling of "shadow build" you must first choose "Clean all" from Build option and after that build your project on "shadow build" disable