.net 单元测试项目找不到被测程序集(或依赖项)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7993469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 16:00:17  来源:igfitidea点击:

Unit test project cannot find assembly under test (or dependencies)

.netvisual-studiovisual-studio-2010unit-testingcompiler-errors

提问by Rhapsody

When I try to run my Unit Test project, I get the following error:

当我尝试运行我的单元测试项目时,出现以下错误:

Could not load file or assembly 'ASSEMBLY_NAME, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies.

The system cannot find the file specified.

无法加载文件或程序集“ASSEMBLY_NAME,版本=1.0.0.0,文化=中性,PublicKeyToken=null”或其依赖项之一。

该系统找不到指定的文件。

The assembly under test is located in the same solution and every project target .NET 4.0 framework. It worked for a while, but today I got this error again. I can't figure out what is wrong, because the error message doesn't tell me enough. (like; WHICH file could not be found..)

被测程序集位于同一个解决方案中,并且每个项目都以 .NET 4.0 框架为目标。它工作了一段时间,但今天我又遇到了这个错误。我无法弄清楚出了什么问题,因为错误消息并没有告诉我足够的信息。(例如;找不到哪个文件..)

I've tried to add a new Test project, but also that one worked for a while before it gave me this same error message.

我试图添加一个新的测试项目,但在它给我同样的错误消息之前,它工作了一段时间。

Is there anybody who can point me in the right direction?

有没有人可以指出我正确的方向?

Edit:In the diagnostic build log, the following error appears:

编辑:在诊断构建日志中,出现以下错误:

C:\Program Files\MSBuild\Microsoft\VisualStudio\v10.0\TeamTest\
Microsoft.TeamTest.targets(14,5): 
error : Could not load file or assembly 'ASSEMBLY_NAME, Version=1.0.0.0, 
Culture=neutral, PublicKeyToken=null' or one of its dependencies. 
The system cannot find the file specified.

Done executing task "BuildShadowTask" -- FAILED. (TaskId:671)

Done building target "ResolveTestReferences" in 
      project "ASSEMBLY_NAME.Tests.vbproj" -- FAILED.: (TargetId:985)

采纳答案by Rhapsody

After checking out some additional search results I came across this question here at SA Private Accessor for method is not found.

在查看了一些其他搜索结果后,我在 SA Private Accessor for method is not found 中遇到了这个问题。

After I deleted the *.accessor file, my test project stopped complaining and compiled succesfully.

删除 *.accessor 文件后,我的测试项目停止抱怨并成功编译。

Everybody thanks for their help!

大家感谢他们的帮助!

回答by Snowbear

Check Build->Configuration Manager...menu. All projects should have the same platform and column Buildchecked.

检查Build->Configuration Manager...菜单。所有项目都应Build检查相同的平台和列。

Also you can take a look into output of the build (Outputwindow), it usually states more clearly which file could not be found.

您也可以查看构建(Output窗口)的输出,它通常会更清楚地说明找不到哪个文件。

回答by jasonoriordan

I tried all above to no avail. Finally, in the csproj i changed

我尝试了以上所有方法都无济于事。最后,在 csproj 中我改变了

<Private>False</Private>

to

<Private>True</Private>

on the references and all tests passed.

在参考和所有测试通过。

回答by JoeBilly

There is a common error about executing unit tests : the execution folder.

执行单元测试有一个常见错误:执行文件夹。

Are you sure your unit test is running in the correct folder ? Not in the famous objfolder ?

您确定您的单元测试在正确的文件夹中运行吗?不在著名的obj文件夹中?

In the objfolder, only generated assemblies are copied, not dependencies -even copy local true-. So if your test is launched from this folder, all the dependencies will missing.

obj文件夹中,只复制生成的程序集,不复制依赖项 - 甚至复制本地 true -。因此,如果您的测试是从此文件夹启动的,则所有依赖项都将丢失。

回答by lolelo

Make sure Dependencies > Edit References > Project (you project) is selected and not .net Assembly of the project

确保选择了依赖项 > 编辑引用 > 项目(您的项目)而不是项目的 .net 程序集

回答by Dan Rayson

I had this error and I fixed it by UPDATING NUGET PACKAGES.

我遇到了这个错误,我通过更新 NUGET PACKAGES修复了它。

This worked because one of my projects in the solution was referencing a very old compiled version of another project that was contained in the same solution.

这是有效的,因为我在解决方案中的一个项目引用了包含在同一解决方案中的另一个项目的非常旧的编译版本。

When I used "Run All" tests in Visual Studio, I suppose it copies over the "current" outputs of the projects, and then the nuget package dependency dlls afterwards, which overwrites the originals if they have the same name. (total guesswork there)

当我在 Visual Studio 中使用“全部运行”测试时,我想它会复制项目的“当前”输出,然后复制 nuget 包依赖项 dll,如果它们具有相同的名称,则会覆盖原始文件。(那里完全是猜测)

When I inspected the error message, I could see that my project was referencing "version 0.0.10" of a dependency, but my solution contained the project all the way up to "version 0.0.30", so massive difference.

当我检查错误消息时,我可以看到我的项目引用了依赖项的“0.0.10 版”,但我的解决方案包含该项目一直到“0.0.30 版”,如此巨大的差异。

回答by Dan Rayson

I had the same issue and found that copying the dll's to the GAC resolved the issue. Though I still don't understand why it doesn't use the referenced projects assemblies.

我遇到了同样的问题,发现将 dll 复制到 GAC 解决了这个问题。虽然我仍然不明白为什么它不使用引用的项目程序集。

回答by Vagaus

Maybe enabling assembly loading logging can give you a hint about why your assembly cannot be loaded.

也许启用程序集加载日志可以提示您为什么无法加载程序集。

http://msdn.microsoft.com/en-us/library/e74a18c4.aspx

http://msdn.microsoft.com/en-us/library/e74a18c4.aspx

回答by Eric Labashosky

Another possible cause is if the assembly name of unit test project is using the same name as the assembly under test. (ie make sure your unit test assembly name is unique).

另一个可能的原因是单元测试项目的程序集名称是否与被测程序集使用相同的名称。(即确保您的单元测试程序集名称是唯一的)。

回答by zzfima

References->Mark dlls->right click->properties->Copy Local set to true

参考->标记dll->右键单击->属性->复制本地设置为true