简体   繁体   English

在构建服务器上运行单元测试时出现 BadImageFormatException

[英]BadImageFormatException when running unit tests on build server

I have a suite of NUnit tests in a project with AnyCPU architecture.我在一个具有 AnyCPU 架构的项目中有一套 NUnit 测试。 Some of these tests use types from an x86 (32-bit) assembly.其中一些测试使用来自 x86(32 位)程序集的类型。

When I run the tests locally (via ReSharper), they all pass.当我在本地(通过 ReSharper)运行测试时,它们都通过了。

However, when they're executed on Jenkins by using nunit3-console MyProject.csproj command, the tests that reference the 32-bit assembly fail with BadImageFormatException:但是,当使用nunit3-console MyProject.csproj命令在 Jenkins 上执行它们时,引用 32 位程序集的测试会失败并出现 BadImageFormatException:

System.BadImageFormatException : Could not load file or assembly '...' or one of its dependencies. An attempt was made to load a program with an incorrect format.

How to make them pass?如何让他们通过?

nunit3-console MyProject.csproj runs the tests in 64-bit process by default , which make it impossible to load 32-bit assemblies. nunit3-console MyProject.csproj默认在 64 位进程中运行测试,这使得无法加载 32 位程序集。 This doesn't have anything to do with build server, so the same would happen on TeamCity etc.这与构建服务器没有任何关系,因此在 TeamCity 等上也会发生同样的情况。

The solution is to force the runner to use 32-bit process by adding a proper flag: nunit3-console MyProject.csproj --x86 .解决方案是通过添加适当的标志来强制运行器使用 32 位进程: nunit3-console MyProject.csproj --x86

Analogous problem with xUnit can be solved by running dotnet xunit -x86 . xUnit 的类似问题可以通过运行dotnet xunit -x86来解决。

I think that for dotnet test the command dotnet test --runtime win10-x86 (with proper system flag) would help.我认为对于dotnet test命令dotnet test --runtime win10-x86 (带有适当的系统标志)会有所帮助。

Why the tests don't fail in ReSharper?为什么 ReSharper 中的测试不会失败?

ReSharper test runner by default automatically chooses the "bittedness" of the process.默认情况下,ReSharper 测试运行程序会自动选择进程的“bittedness”。 Maybe it analyses assembly references in test files, I don't know.也许它会分析测试文件中的程序集引用,我不知道。 Anyway, in my case it was choosing 32-bit runner for my unit tests.无论如何,就我而言,它为我的单元测试选择了 32 位运行程序。 When I forced it to use 64-bit (Unit Test Sessions —> Options —> Platform —> x64), a few tests failed with same message as on Jenkins.当我强制它使用 64 位(单元测试会话 -> 选项 -> 平台 -> x64)时,一些测试失败,并显示与 Jenkins 上相同的消息。

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

相关问题 在构建服务器上使用Fakes运行单元测试的异常 - Exception running unit tests with Fakes on build server 在TFS构建服务器上运行单元测试伪造框架| 错误 - Running Unit Tests Fakes Framework on TFS build server | Error 运行应用程序时出现BadImageFormatException - BadImageFormatException when running application 我正在获取System.BadImageFormatException:操作系统在运行单元测试时无法运行异常 - I am getting System.BadImageFormatException : The operating system cannot run exception while running unit tests 单元测试仅在构建服务器上失败 - Unit tests failing only on the build server TFS 2013构建:在多个构建代理上并行运行单元测试 - TFS 2013 Build: Running Unit Tests in Parallel on multiple build agents 运行单元(集成)测试时,ContextSwitch死锁 - ContextSwitchDeadlock when running unit (integration) tests 在构建服务器上运行时跳过某些测试 - Skip certain tests while running on the build server Visual Studio 单元测试在 TFS 构建上运行速度较慢 - Visual Studio Unit Tests running slower on TFS Build 在 VS 2017 中针对完整的 .net 框架 asp.net 核心项目运行 XUnit 2.2.0 单元测试时出现 System.BadImageFormatException - System.BadImageFormatException when running XUnit 2.2.0 unit test in VS 2017 against a full .net framework asp.net core project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM