2016-05-18 53 views
1

我開始a new project。它是一個C#類庫,並附帶一些用戶可以使用和擴展的窗口。用Windows窗體的C#測試在Mono/Travis上失敗

我在Windows上用NUnit編寫並運行了some unit tests。我的測試中沒有任何一項能夠打電話給form.Show()。測試全部通過Windows上的TestDriven.NET。

當特拉維斯運行下我的Linux上的泊塢窗容器內測試,我得到一個異常堆棧:

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

構建節點顯然沒有顯示,因爲它是無頭。

有沒有什麼辦法讓我的測試在這種環境下運行?

回答

1

我交叉發佈到GitHub(Travis-CI回購),並從AlexanderKö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 

然後測試通過。