Resharper runs UnitTest from different location

前端 未结 10 713
清歌不尽
清歌不尽 2020-12-01 15:46

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

相关标签:
10条回答
  • 2020-12-01 16:00

    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.

    0 讨论(0)
  • 2020-12-01 16:02

    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

    0 讨论(0)
  • 2020-12-01 16:09

    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));
    }
    
    0 讨论(0)
  • 2020-12-01 16:11

    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.

    0 讨论(0)
  • 2020-12-01 16:14

    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 )
    }
    
    0 讨论(0)
  • 2020-12-01 16:19

    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.

    0 讨论(0)
提交回复
热议问题