ashes999
ashes999

Reputation: 10163

C# tests with Windows Forms fails on Mono/Travis

I started a new project. It's a C# class library, and ships with a bunch of windows Form classes that the user can use and extend.

I wrote and ran some unit tests on Windows with NUnit. None of my tests go as far as calling form.Show(). The tests all pass with TestDriven.NET on Windows.

When Travis runs my tests under a docker container on Linux, I get an exception stack:

System.TypeInitializationException : The type initializer for 'System.Windows.Forms.WindowsFormsSynchronizationContext' threw an exception. ----> System.TypeInitializationException : The type initializer for 'System.Windows.Forms.XplatUI' threw an exception. ----> System.ArgumentNullException : Could not open display (X-Server required. Check your DISPLAY environment variable) Parameter name: Display at System.Windows.Forms.Control..ctor () <0x40965cf0 + 0x0005e> in <filename unknown>:0 at System.Windows.Forms.ScrollableControl..ctor () <0x409656f0 + 0x00017> in <filename unknown>:0 at System.Windows.Forms.ContainerControl..ctor () <0x40965520 + 0x0002d> in <filename unknown>:0 at System.Windows.Forms.Form..ctor () <0x40964bd0 + 0x000ef> in <filename unknown>:0 at Chrysalis.Core.Forms.ChrysalisForm..ctor () <0x40961790 + 0x00017> in <filename unknown>:0 at Chrysalis.Core.UnitTests.Forms.ChrysalisFormTests+TestChrysalisForm..ctor () <0x40961760 + 0x00014> in <filename unknown>:0 at (wrapper remoting-invoke-with-check) Chrysalis.Core.UnitTests.Forms.ChrysalisFormTests+TestChrysalisForm:.ctor () at Chrysalis.Core.UnitTests.Forms.ChrysalisFormTests.CancelButtonMovesWhenResizeEventFires () <0x409614f0 + 0x0003e> in <filename unknown>:0 at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&) at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) <0x4090c360 + 0x000b7> in <filename unknown>:0 --TypeInitializationException at System.Windows.Forms.Theme.get_MenuAccessKeysUnderlined () <0x4096bcb0 + 0x00010> in <filename unknown>:0 at System.Windows.Forms.SystemInformation.get_MenuAccessKeysUnderlined () <0x40967210 + 0x0001c> in <filename unknown>:0 at System.Windows.Forms.Control..ctor () <0x40965cf0 + 0x00274> in <filename unknown>:0 at (wrapper remoting-invoke-with-check) System.Windows.Forms.Control:.ctor () at System.Windows.Forms.WindowsFormsSynchronizationContext..cctor () <0x40967100 + 0x00031> in <filename unknown>:0 --ArgumentNullException at System.Windows.Forms.XplatUIX11.SetDisplay (IntPtr display_handle) <0x4096e400 + 0x00ec7> in <filename unknown>:0 at System.Windows.Forms.XplatUIX11..ctor () <0x4096c210 + 0x00231> in <filename unknown>:0 at System.Windows.Forms.XplatUIX11.GetInstance () <0x4096c000 + 0x00081> in <filename unknown>:0 at System.Windows.Forms.XplatUI..cctor () <0x4096bd20 + 0x00170> in <filename unknown>:0

The build node obviously doesn't have a display, because it's headless.

Is there any way to get my tests to run under this environment?

Upvotes: 1

Views: 850

Answers (1)

ashes999
ashes999

Reputation: 10163

I cross-posted this to GitHub (Travis-CI repo) and got an answer from Alexander Köplinger.

Travis supports using xvfb as a GUI back-end. I just needed to add this to my .travis.yml file:

before_script:
  - "export DISPLAY=:99.0"
  - "sh -e /etc/init.d/xvfb start"
  - sleep 3 # give xvfb some time to start

The tests then passed.

Upvotes: 1

Related Questions