| Revision History | |||
|---|---|---|---|
| 18 April 2014 | Modified the SWTBot test so that it can be reused also in a test suite (see the comments to this post). | ||
I happened to give a lecture at the University of Florence on Test Driven Development; besides the standard Junit tests I wanted to show the students also some functional tests withSWTBot. However, I did not want to introduce Eclipse views or dialogs, I just wanted to test a plain SWT application with SWTBot.
In the beginning, it took me some time to understand how to do that (I had always used SWTBot in the context of an Eclipse application); thanks toMickael Istria, who assisted me via Skype, it ended up being rather easy.
You can find this example here: https://github.com/LorenzoBettini/junit-swtbot-example.
The SWT application is a simple dialog that computes the factorial of the given input (nothing fancy, its code can be seenhere).
If we now want to test this SWT application with SWTBot, we can write an abstract base class that we use for our tests (see also the online code)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | packagemathutils.ui.tests; importjava.util.concurrent.BrokenBarrierException; importjava.util.concurrent.CyclicBarrier; importmathutils.ui.MathUtilsWindow; importorg.eclipse.swt.widgets.Display; importorg.eclipse.swt.widgets.Shell; importorg.eclipse.swtbot.swt.finder.SWTBot; importorg.eclipse.swtbot.swt.finder.SWTBotTestCase; importorg.junit.After; importorg.junit.Before; importorg.junit.BeforeClass; publicabstractclassAbstractMathUtilsWindowTestextendsSWTBotTestCase{ protectedSWTBotbot; protectedstaticThreaduiThread; protectedstaticShellshell; privatefinalstaticCyclicBarrierswtBarrier=newCyclicBarrier(2); @BeforeClass publicstaticvoidsetupApp(){ if(uiThread==null){ uiThread=newThread(newRunnable(){ @Override publicvoidrun(){ try{ while(true){ // open and layout the shell MathUtilsWindowwindow=newMathUtilsWindow(); window.open(); shell=window.getShell(); // wait for the test setup swtBarrier.await(); // run the event loop window.eventLoop(Display.getDefault()); } }catch(Exceptione){ e.printStackTrace(); } } }); uiThread.setDaemon(true); uiThread.start(); } } @Before publicfinalvoidsetupSWTBot()throwsInterruptedException,BrokenBarrierException{ // synchronize with the thread opening the shell swtBarrier.await(); bot=newSWTBot(shell); } @After publicvoidcloseShell()throwsInterruptedException{ // close the shell Display.getDefault().syncExec(newRunnable(){ publicvoidrun(){ shell.close(); } }); } protectedvoidassertResultGivenInput(Stringinput,StringexpectedResult){ bot.textWithLabel("input").setText(input); bot.button("Compute").click(); assertEquals(expectedResult,bot.textWithLabel("result").getText()); } } |
And we use this base class in our tests, for instance
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | packagemathutils.ui.tests; importorg.junit.Test; importorg.junit.runner.RunWith; importorg.junit.runners.BlockJUnit4ClassRunner; @RunWith(BlockJUnit4ClassRunner.class) publicclassMathUtilsNonValidInputWindowTestextendsAbstractMathUtilsWindowTest{ @Test publicvoidtestEmptyInput(){ assertResultGivenInput(" ","Empty input"); } @Test publicvoidtestNonValidIntegerInput(){ assertResultGivenInput("foo","Not a valid input"); } @Test publicvoidtestNonValidInput(){ assertResultGivenInput("-1","Not a valid input"); } } |
There are a few things to note in the abstract base class:
- You need to spawn the application in a new thread (the bot will run in a different thread)
- You must start the application before creating the bot (otherwise the Display will be null)
- after that you can simply use SWTBot API as you’re used to.
Note that the thread will create our window and then it will enter the event loop; this thread synchronizes with the@Beforemethod (executed before each test), which creates the SWTBot (using the shell created by the thread). The @After method (executed after each test), will close our window, so that each test is independent from each other. The thread executes in an infinite loop, thus as soon as the shell is closed it will create a new one, etc.
Of course,this must be executed as a “Junit test”,NOT as a “Plug-in Junit test”, neither as a “SWTBot Test”, since we do not want any Eclipse application while running the test:
In the sources of the example you can find also the files to run the tests headlessly with Buckminster or with Maven/Tycho. Just enter the directorymathutils.buildand
1 | ant-fbuild.ant |
for Buckminster or
1 | mvncleanverify |
for Maven.
For Buckminster, you just need to save the launch configuration you used to run the test, and use the junit command:
1 | junit-l"mathutils.ui.tests/mathutils.ui.tests.launch"--flatXML-o"${buckminster.output.root}/test.results/mathutils.ui.tests.launch.result.xml" |
For Tycho, you must specify <packaging>eclipse-test-plugin</packaging>,but without further configuration. This will defaultuseUIHarnessto false.
During the headless run, first the Junit tests for the implementation of the factorial will be executed (these are not interesting in the context of SWTBot) and then the SWTBot tests will be executed.

