[英]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 等上也会发生同样的情况。
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
(带有适当的系统标志)会有所帮助。
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.