问题
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