使用 Windows 表单的 C# 测试在 Mono/Travis 上失败
C# tests with Windows Forms fails on Mono/Travis
我开始了a new project。它是一个 C# class 库,附带了一堆 windows Form
class 供用户使用和扩展。
我用 NUnit 在 Windows 上写了 运行 some unit tests。 None 我的测试进行到调用 form.Show()
。 Windows.
测试全部通过 TestDriven.NET
当 Travis 运行 在 Linux 上的 docker 容器下进行我的测试时,我得到一个异常堆栈:
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
构建节点显然没有显示,因为它是无头的。
在这种环境下有什么方法可以让我的测试达到 运行 吗?
我将其交叉发布到 GitHub(Travis-CI 存储库)并得到了 Alexander Köplinger 的答复。
Travis 支持使用 xvfb
作为 GUI 后端。我只需要将它添加到我的 .travis.yml
文件中:
before_script:
- "export DISPLAY=:99.0"
- "sh -e /etc/init.d/xvfb start"
- sleep 3 # give xvfb some time to start
然后测试通过了。
我开始了a new project。它是一个 C# class 库,附带了一堆 windows Form
class 供用户使用和扩展。
我用 NUnit 在 Windows 上写了 运行 some unit tests。 None 我的测试进行到调用 form.Show()
。 Windows.
当 Travis 运行 在 Linux 上的 docker 容器下进行我的测试时,我得到一个异常堆栈:
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
构建节点显然没有显示,因为它是无头的。
在这种环境下有什么方法可以让我的测试达到 运行 吗?
我将其交叉发布到 GitHub(Travis-CI 存储库)并得到了 Alexander Köplinger 的答复。
Travis 支持使用 xvfb
作为 GUI 后端。我只需要将它添加到我的 .travis.yml
文件中:
before_script:
- "export DISPLAY=:99.0"
- "sh -e /etc/init.d/xvfb start"
- sleep 3 # give xvfb some time to start
然后测试通过了。