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

发布于 2024-12-13 17:24:22 字数 884 浏览 3 评论 0原文

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

无法加载文件或程序集“ASSEMBLY_NAME、Version=1.0.0.0、Culture=neutral、PublicKeyToken=null”或其依赖项之一。

系统找不到指定的文件。

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

我尝试添加一个新的测试项目,但该项目也工作了一段时间,然后才给我同样的错误消息。

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

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

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)

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.

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..)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

玩物 2024-12-20 17:24:22

检查构建->配置管理器...菜单。所有项目都应具有相同的平台并检查Build列。

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

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

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

峩卟喜欢 2024-12-20 17:24:22

在检查了一些其他搜索结果后,我在 SA ​​Private Accessor for找不到方法

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

大家感谢他们的帮助!

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

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

Everybody thanks for their help!

⊕婉儿 2024-12-20 17:24:22

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

<Private>False</Private>

<Private>True</Private>

参考文献,并且所有测试都通过了。

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

<Private>False</Private>

to

<Private>True</Private>

on the references and all tests passed.

逐鹿 2024-12-20 17:24:22

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

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

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

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 obj folder ?

In the obj folder, 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.

破晓 2024-12-20 17:24:22

确保依赖关系>编辑参考文献>选择项目(您的项目)而不是项目的 .net 程序集

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

舟遥客 2024-12-20 17:24:22

也许启用程序集加载日志记录可以提示您程序集无法加载的原因。

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

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

冷情妓 2024-12-20 17:24:22

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

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.

゛清羽墨安 2024-12-20 17:24:22

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

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).

小霸王臭丫头 2024-12-20 17:24:22

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

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

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

当我检查错误消息时,我可以看到我的项目正在引用依赖项的“版本 0.0.10”,但我的解决方案包含该项目一直到“版本 0.0.30”,如此庞大不同之处。

I had this error and I fixed it by UPDATING 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)

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.

誰認得朕 2024-12-20 17:24:22

就我而言,单元测试项目文件需要与目标框架和目标平台的主项目文件匹配:

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<PlatformTarget>x86</PlatformTarget>

In my case the unit test project file needed to match the main project file for both target framework and target platform:

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<PlatformTarget>x86</PlatformTarget>
千寻… 2024-12-20 17:24:22

引用->标记 dlls->右键单击->属性->复制本地设置为 true

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文