简体   繁体   English

Resharper-NUnit-无法运行单元测试,因为代码和测试项目二进制文件位于不同的位置

[英]Resharper - NUnit - unable to run unit tests because code and test project binaries in different locations

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. 我的解决方案中有2个项目,一个是实际代码( NUnitSample1 ),而另一个是带有单元测试的测试项目( NUnitSample1.Test )。 NUnitSample1.Test contains a reference of the first project NUnitSample1 . NUnitSample1.Test包含第一个项目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 . NUnitSample1项目引用的CopyLocal属性需要设置为false

Now, when I build and try to run the unit test using ReSharper, it fails with the following message: 现在,当我构建并尝试使用ReSharper运行单元测试时,它失败并显示以下消息:

System.IO.FileNotFoundException : Could not load file or assembly 'NUnitSample1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. System.IO.FileNotFoundException:无法加载文件或程序集'NUnitSample1,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null'或其依赖项之一。 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? 有什么方法可以在保持此结构的同时使用ReSharper运行测试吗? 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. 但是,动态加载程序集(如AlexeiLevenkov所建议)的工作需要单独设置每种方法,这是不可行的。

+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. 我找到了可以在其中指定多个程序集的NUnit链接 ,但是,当我尝试使用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. TIA。

I was able to load the missing assembly using the answer from here in the TestFixtureSetup (will also work in the Setup method). 我能够加载缺少的组件使用的答案从这里TestFixtureSetup (也将在工作中Setup方法)。

[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. 可以修改LoadFromSameFolder方法中的上述代码以精确地定位程序集。

PS: Thanks to Alexei Levenkov for putting me on the right path. PS:感谢Alexei Levenkov让我走上正确的道路。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM