27
Introduction to Functional Testing with SWTBot and Maven/Tycho Lorenzo Bettini, DISIA, Univ. Florence, Italy Mickael Istria, Red Hat Inc.

Introduction to Functional Testing with SWTBot and … · Introduction to Functional Testing with SWTBot and ... Do I really need a running Eclipse to test this and that? ... Code

Embed Size (px)

Citation preview

Page 1: Introduction to Functional Testing with SWTBot and … · Introduction to Functional Testing with SWTBot and ... Do I really need a running Eclipse to test this and that? ... Code

Introduction to Functional Testing with SWTBot and Maven/Tycho

Lorenzo Bettini, DISIA, Univ. Florence, Italy

Mickael Istria, Red Hat Inc.

Page 2: Introduction to Functional Testing with SWTBot and … · Introduction to Functional Testing with SWTBot and ... Do I really need a running Eclipse to test this and that? ... Code

What is SWTBot?

SWTBot is a simple Java API to interact with SWT widgets -and other typical Eclipse RCP UI artifacts-, using the usual vocabulary of high level UI operations.

Page 3: Introduction to Functional Testing with SWTBot and … · Introduction to Functional Testing with SWTBot and ... Do I really need a running Eclipse to test this and that? ... Code

Functional Testing● “UI logic” testing● Testing the behavior of the application with respect to user interactions

Page 4: Introduction to Functional Testing with SWTBot and … · Introduction to Functional Testing with SWTBot and ... Do I really need a running Eclipse to test this and that? ... Code

Software/Application Under Test (SUT/AUT)SWTBot is good for (and only for) SWT-based applications:

● with plain Java/SWT or● Eclipse RCP.

ie. No SWT => No SWTBot

Page 5: Introduction to Functional Testing with SWTBot and … · Introduction to Functional Testing with SWTBot and ... Do I really need a running Eclipse to test this and that? ... Code

What isn’t SWTBot?

SWTBot is not a test framework.

It can be used to write tests in whatever test framework, orchestrators (JUnit, TestNG...) and runtime (PDE Tests, Tycho surefire...)

Page 6: Introduction to Functional Testing with SWTBot and … · Introduction to Functional Testing with SWTBot and ... Do I really need a running Eclipse to test this and that? ... Code

Dont’s● SWTBot does NOT replace plain JUnit Tests● Always ask yourself “Do I need functional testing?”

○ It takes much longer to execute (you need a running Eclipse)○ Do I really need a running Eclipse to test this and that?

Page 7: Introduction to Functional Testing with SWTBot and … · Introduction to Functional Testing with SWTBot and ... Do I really need a running Eclipse to test this and that? ... Code

Target SWTBot usersSWTBot APIs mainly target SWT Developers who want an efficient way to write UI Tests, in a grain that’s close to the user actions.

The Test Recorder and Generator,

● may be used directly by end-users to easily record what happens and share it with developers as “Steps to Reproduce”.

● might also be considered in order to implement some macro support in Eclipse RCP applications, although it’s not its target.

Page 8: Introduction to Functional Testing with SWTBot and … · Introduction to Functional Testing with SWTBot and ... Do I really need a running Eclipse to test this and that? ... Code

An SWTBot test● SWTBot provides a fluent Java API

○ Hides most of the complexity of SWT and Eclipse○ Wraps SWT components into Java classes○ Methods for

■ Simulating user interaction■ Accessing SWT widgets

● Content-assist is your friend!

Page 9: Introduction to Functional Testing with SWTBot and … · Introduction to Functional Testing with SWTBot and ... Do I really need a running Eclipse to test this and that? ... Code

Custom runners

A screenshot will be automatically taken and saved for each failing test

Page 10: Introduction to Functional Testing with SWTBot and … · Introduction to Functional Testing with SWTBot and ... Do I really need a running Eclipse to test this and that? ... Code

Setup and Teardown:

Page 11: Introduction to Functional Testing with SWTBot and … · Introduction to Functional Testing with SWTBot and ... Do I really need a running Eclipse to test this and that? ... Code

What it looks like?

SWTBot generates and sends to the AUT/SUT corresponding SWT events for the UI operations.

TODO: Code snippet

Page 12: Introduction to Functional Testing with SWTBot and … · Introduction to Functional Testing with SWTBot and ... Do I really need a running Eclipse to test this and that? ... Code

SWTBot understands the “workbench” grainViews, perspectives… You can also use them rather than widgets.

Page 13: Introduction to Functional Testing with SWTBot and … · Introduction to Functional Testing with SWTBot and ... Do I really need a running Eclipse to test this and that? ... Code

DEMO Time

Page 14: Introduction to Functional Testing with SWTBot and … · Introduction to Functional Testing with SWTBot and ... Do I really need a running Eclipse to test this and that? ... Code

SWTBot also has APIs for● Nebula NatTable, Gallery...● GEF: Edit Parts

Page 15: Introduction to Functional Testing with SWTBot and … · Introduction to Functional Testing with SWTBot and ... Do I really need a running Eclipse to test this and that? ... Code

✘Long operations?

Don’t wait blindly: Thread.sleep(20000)

Explicitly wait for what you want!

Searching, timeout, and wait conditions

Page 16: Introduction to Functional Testing with SWTBot and … · Introduction to Functional Testing with SWTBot and ... Do I really need a running Eclipse to test this and that? ... Code

Other Dont’s● Example

○ For your tests, you need to get rid of the Welcome page (if present)○ Don’t do that like that:

■ It’s not part of your functional test■ You waste time due to timeouts

Page 17: Introduction to Functional Testing with SWTBot and … · Introduction to Functional Testing with SWTBot and ... Do I really need a running Eclipse to test this and that? ... Code

Do that programmaticallyYou won’t waste time for timeouts

Page 18: Introduction to Functional Testing with SWTBot and … · Introduction to Functional Testing with SWTBot and ... Do I really need a running Eclipse to test this and that? ... Code

You need the Java perspective......for testing your views?

● Don’t open that simulating user interactions● That is not part of your functional test

Page 19: Introduction to Functional Testing with SWTBot and … · Introduction to Functional Testing with SWTBot and ... Do I really need a running Eclipse to test this and that? ... Code

Do that programmaticallyRecall that SWTBot runs on a non-UI thread, thus PlatformUI.getWorkbench() will result with a NullPointerException

Page 20: Introduction to Functional Testing with SWTBot and … · Introduction to Functional Testing with SWTBot and ... Do I really need a running Eclipse to test this and that? ... Code

Hands off!● While an SWTBot is running, keep your hands off!● Otherwise SWTBot will not be able to interact correctly with the AUT.● The AUT must have the focus.

TIP: in Linux you can run SWTBot tests in another DISPLAY

● Use Xephyr and a Window Manager inside (e.g., icewm, or similar)

Page 21: Introduction to Functional Testing with SWTBot and … · Introduction to Functional Testing with SWTBot and ... Do I really need a running Eclipse to test this and that? ... Code

Execution & AutomationAs it relies on regular test frameworks, SWTBot tests can very easily be run with usual build technologies such as PDE/Build and Tycho. Just DON’T RUN IN THE UI THREAD

Page 22: Introduction to Functional Testing with SWTBot and … · Introduction to Functional Testing with SWTBot and ... Do I really need a running Eclipse to test this and that? ... Code

Test Recorder and GeneratorPrinciple: the other way round, estimate what is the user action for a chain of SWT events.

Usage:

1. Make sure you have the recorder feature Installed2. File > New > SWTBot > Test from recorder (TODO)3. Decide of your AUT: current workbench or Run Configuration4. Ok5. Start recording6. Reuse code

Page 23: Introduction to Functional Testing with SWTBot and … · Introduction to Functional Testing with SWTBot and ... Do I really need a running Eclipse to test this and that? ... Code

DEMO Time

Page 24: Introduction to Functional Testing with SWTBot and … · Introduction to Functional Testing with SWTBot and ... Do I really need a running Eclipse to test this and that? ... Code

Recorder - caveatsSuboptimal “addressing”

Generated code is pretty basic, (doesn’t use intermediary variables nor general software best practices) => you usually have to refactor it.

Generator doesn’t know nor add wait conditions.

So…

Recorder is good to get a 1st iteration of a test, but some SWT developer almost always have to rework it.

Page 25: Introduction to Functional Testing with SWTBot and … · Introduction to Functional Testing with SWTBot and ... Do I really need a running Eclipse to test this and that? ... Code

SWTBot in CI environmentsNeed Window Manager (icewm, ...)

sudo: false language: java jdk: oraclejdk7 cache: directories: - $HOME/.m2 env: DISPLAY=:99.0 install: true addons: apt: packages: - metacity before_script: - sh -e /etc/init.d/xvfb start - metacity --sm-disable --replace 2> metacity.err & script: - export - mvn -f swtbot.example.parent/pom.xml clean verify

Page 26: Introduction to Functional Testing with SWTBot and … · Introduction to Functional Testing with SWTBot and ... Do I really need a running Eclipse to test this and that? ... Code

Contributinghttps://wiki.eclipse.org/SWTBot/Contributing

● Oomph setup to easily get you started● Push to Gerrit for review

○ Automatically triggers Hudson build job