问题
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?
回答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.
回答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;
}
回答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.
回答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.
回答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 )
}
回答6:
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.
回答7:
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));
}
回答8:
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
回答9:
What solved it for me was to set the "Copy Local" property to true on the nunit.framework.dll reference in the test project.
回答10:
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
来源:https://stackoverflow.com/questions/16231084/resharper-runs-unittest-from-different-location