Resharper runs UnitTest from different location

20,982

Solution 1

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.

Solution 2

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;
    }

Solution 3

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.

Solution 4

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.

Solution 5

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 )
}
Share:
20,982

Related videos on Youtube

Roar
Author by

Roar

Updated on September 17, 2020

Comments

  • Roar
    Roar over 3 years

    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?

  • Roar
    Roar about 11 years
    it happens with unit tests but not with nunit, so what's the different, any ideas? @tallseth
  • Roar
    Roar about 11 years
    I reference needed assembly, it works fine if NUnit test, but not work with default Visual Studio test Microsoft.VisualStudio.TestTools.UnitTesting;
  • Dio F
    Dio F about 11 years
    No idea so far. BTW: The Microsoft.VisualStudio.TestTools.UnitTesting functionality is often referred to as "MSTest". Maybe this helps getting better search results.
  • tallseth
    tallseth about 11 years
    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?
  • Roar
    Roar about 11 years
    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
  • Remy
    Remy almost 11 years
    I've disabled shadow-copy, but still have the same issue. It works if I run it from Test Explorer, but not via Resharper.
  • tallseth
    tallseth almost 11 years
    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: knot.org/scottlaw/blog/?p=404
  • Daniel Bişar
    Daniel Bişar almost 11 years
    Sadly it is not fixed with Resharper 7.1.3 and VS 2012
  • mcdon
    mcdon almost 8 years
    Came accross TestContext.CurrentContext.TestDirectory. I'm using it now in NUnit 3.0. I don't know how far back TestContext.CurrentContext.TestDirectory was available.
  • Holf
    Holf over 7 years
    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.
  • Alex M
    Alex M about 6 years
    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.