BadImageFormatException when running unit tests on build server

我只是一个虾纸丫 提交于 2019-12-02 22:13:40

问题


I have a suite of NUnit tests in a project with AnyCPU architecture. Some of these tests use types from an x86 (32-bit) assembly.

When I run the tests locally (via ReSharper), they all pass.

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:

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?


回答1:


nunit3-console MyProject.csproj runs the tests in 64-bit process by default, which make it impossible to load 32-bit assemblies. This doesn't have anything to do with build server, so the same would happen on TeamCity etc.

The solution is to force the runner to use 32-bit process by adding a proper flag: nunit3-console MyProject.csproj --x86.

Analogous problem with xUnit can be solved by running dotnet xunit -x86.

I think that for dotnet test the command dotnet test --runtime win10-x86 (with proper system flag) would help.

Why the tests don't fail in ReSharper?

ReSharper test runner by default automatically chooses the "bittedness" of the process. 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. When I forced it to use 64-bit (Unit Test Sessions —> Options —> Platform —> x64), a few tests failed with same message as on Jenkins.



来源:https://stackoverflow.com/questions/58875801/badimageformatexception-when-running-unit-tests-on-build-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!