2

xUnit tests run under Jenkins.

I run tests for x86/x64 Debug/Release configurations. Some configuration fails with error when running under Jenkins.

Run command and error message follows:

packages\xunit.runner.console.2.1.0\tools\xunit.console.exe bin\x86\Release\MyDllName.dll -verbose -parallel none -diagnostics

xUnit.net Console Runner (64-bit .NET 4.0.30319.42000)

System.BadImageFormatException: Could not load file or assembly 'MyDllName, Version=1.3.549.4300, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format.
<Absolute-path-to-file>\RunTests.1.proj(59,5): error MSB3073: The command "packages\xunit.runner.console.2.1.0\tools\xunit.console.exe bin\x86\Release\MyDllName.dll -verbose -parallel none -diagnostics" exited with code 1.

When I run the same batch file from command line as I use to run Jenkins job, everything work fine.

I tried to run Jenkins as service and as standalone application. I got error in both cases.

I tried to run ILSpy on this dll and did not found some error messages concerning missing dependencies.

How to make xUnit run successfully on all configurations under Jenkins?

My dll built with .NET 4.6.1.
Jenkins version: 2.32.1, but I have the same issues with older versions.
Server OS: Windows 2008 R2 Enterprise.
xunit.runner.console: 2.1.0

sergtk
  • 10,714
  • 15
  • 75
  • 130
  • For people with same problem but with Nunit — have a look at this question: https://stackoverflow.com/questions/58875801/badimageformatexception-when-running-unit-tests-on-build-server/58875802#58875802 – Павле Nov 15 '19 at 11:19

1 Answers1

2

This is likely the result of the DLL in question having a 32-bit bittedness constraint.

You'll need to either

a. run the 32-bit runner (if for example some dep requires a 32 bit DLL) by using dotnet xunit -x86 command,

or

b. build the test assembly for AnyCpu [or x64] (but using the "Prefer 32 bit" flag). Keep in mind the flag is available only in .NET 4.5, not in 4.6.

See lots more similar cases in this answer

Павле
  • 800
  • 5
  • 20
Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
  • I am wondering why the error reproduced under Jenkins only? If I run the same script from command line, it is ok. x86/Release works fine (in most times :) - I need yet to investigate when exactly ). x86/Debug produces error always – sergtk Jan 29 '17 at 05:33
  • 1
    @sergtk There's little Jenkins (or even xUnit) specific in the problem - this is a low level .NET issue - a DLL that says "I *must* be in an x86 process", and .NET is telling all and sundry "well, I'm afraid this is already an x64 process, so change your process or change your process". The chances are you have a variation in settings (AnyCpu or not) between the Debug and Release builds (the settigng is configuration specific) – Ruben Bartelink Jan 29 '17 at 05:48
  • Running xunit.console.x86.exe resolved issue. I don't understand all the details why xunit.console.exe didn't work for x64 under Jenkins only, but anyway issue solved! – sergtk Jan 31 '17 at 20:10
  • 1
    @sergtk the reason it's working now is because your Debug build is x64 or AnyCpu and your Release is x86. This means the Jenkins build is happy (because it's release and that matches the runner you are pairing it with). If you go into the project settings for your test project and pick the Build COnfiguration drop down, you'll see the problem – Ruben Bartelink Jan 31 '17 at 21:02
  • Why everything was fine if I run xunit.console.exe from console, not from jenkins? The issue is only reproduced in Jenkins – sergtk Jan 31 '17 at 21:08
  • 1
    @sergtk There is no magic or mystery - processes are x86 or x64. DLLs either say x64, x86b or AnyCpu. Release and Debug builds can have diff options. Jenkins runs exes and grabs stdout and stderr. Use Assembly Binding Viewer to debug. (I dont know exactly which one of the above is the issue, but it's going to be one of them) – Ruben Bartelink Jan 31 '17 at 22:32
  • The second way ("Prefer 32 bit" flag) doesn't work for me on .NET 4.6.1. It's a disabled setting. According to the answer below, it's enabled only in .NET 4.5: https://stackoverflow.com/questions/16546886/why-is-the-checkbox-prefer-32-bit-disabled-in-visual-studio-2012 – Павле Nov 15 '19 at 08:38