Easiest way to unit test SWT and Swing apps in a headless environment?
I'm looking to unit test some SWT and Swing code for a project I'm working on and the tests run fine as long as I开发者_如何学C'm running them from eclipse.
As soon as I run them in my hudson environment it fails since hudson runs the tests in headless mode.
What's the best way of doing this? Open source solutions only please (since the project is open source).
You could run Xvfb (X virtual framebuffer, an X11 server that performs all graphical operations in memory) and this works fine.
But there is another solution with Hudson's plugin for Xvnc. Simply install the plugin and check the checkbox in the job configuration screen:
alt text http://www.justinedelson.com/blog/wp-content/uploads/xvnc_box.jpg
Hudson will then automatically start up a Xvnc session and set the DISPLAY environment variable to the appropriate value and then shut down the session when the build is complete. One advantage this has over the Xvfb method is that if you have multiple Swing/SWT projects building simultaneously, each build has its own X session. This may not be an issue at all but it seems like a good idea.
Before using this plugin, you obviously have to have Xvnc installed. What's less obvious (although sensible) is that you must also set a password. You do this by running:
$ vncpassword
This has to be done as the same user Hudson runs as.
Try the Abbot Java GUI Testing Framework and SWTbot. At least SWTbot should be able to do it.
If neither offers a headless mode, then this blog post might give you some ideas how to get rid of the UI for testing.
Using Swing I tend to organise things so that the component tree can be created without a Window at the top. Doing this allows you to simply create a JPanel in a unit test and use that as your top-level component. There are certain things you cannot test, such as focus and any logic involved in the creation of the Frame for normal operation, but the vast majority can be tested.
You may want to look into the FEST library to make life easier whether you go headless or not, it looks very good: http://fest.easytesting.org/swing/wiki/pmwiki.php
I was sure I posted this here before, not sure what happened to it.
Cacio allows for running Swing app headless.
http://rkennke.wordpress.com/2011/10/17/cacio-for-ui-testing/
I don't know about SWT, but with Swing you can't. Any instantiation of a Window (JFrame, JDialog, etc.) even if it is never set to visible will blow up in headless mode (on JDK 5). What we did was not run in headless mode and install Xvfb to provide the windowing without actually having a real windowing system installed.
You could try RedDeer Testing framework https://github.com/jboss-reddeer/reddeer
• Support for running tests in a Jenkins CI environment (Hudson like)
精彩评论