Marathon Tutorial – v1.0.2

Jean Lazarou
Novembre 8, 2006

Preface

Marathon is a tool that helps writing user acceptance tests. This tutorial will cover different aspects of development assisted by tests.

W're going to start with an example of writing user acceptance tests against an application already developed, the calculator application.

In the “tutorial” directory you will find one subdirectory per step.

This version of the tutorial is based on Marathon 1.0.2 you can download from : http://www.marathontesting.com/marathon/downloads/. The tutorial was written on a Windows system (tested on Windows XP).

Calculator example.

A calculator application was developed and the result is bundled in a jar file (tutorial/lib/calculator.jar). Launch it just by double-clicking1 the jar file. Play with the calculator, next figure shows the application:

Step 1: Write some acceptance tests

We want to write tests for the calculator. We expect the calculator to be able to:
Let's write a test script to test addition.

Step 2: Refactor the Tests

As the Marathon acceptance tests are written in Python scripting language it helps managing the tests as you would do in any programming development: make maintenance easier and reuse of code.
Can we refactor anything in the three test scripts? Let's try. What can we refactor? What don't we like right now?
Wouldn't be easier (or nicer) to write:
We need to create some helper methods: enter, add, subtract, clear, assertResult and assertCleared. As we want them to be used by all the scripts, we're going to create a new Python module:

tutorial/step2/Module/util.py
from marathon.playback import *

def enter(value):
    window('Calculator Example')
    enterValue(value)

def add(value):
    click('+')
    enterValue(value)

def subtract(value):
    click('-')
    enterValue(value)

def clear():
    click('C')

def assertResult(expected):
    click('=')
    assertText('JTextField', expected)

def assertCleared():
    assertText('JTextField', '0')

def enterValue(value):
    for char in value:
            click(char)
Here is some explanation about this script:
Now, we can rewrite the three test scripts as:
testAdd.py
from Modules.util import *

useFixture(default)

def test():
    enter('12')
    add('4')
    assertResult('16')
    close()


testSubstract.py
from Modules.util import *

useFixture(default)

def test():
    enter('12')
    subtract('4')
    assertResult('8')
    close()



testClear.py
from Modules.util import *

useFixture(default)

def test():
    enter('12')
    subtract('4')
    assertResult('8')
    clear()
    assertCleared()
    close()


Step 3: More Tests...

Now we can write more tests, without even recording user actions.
testAddAndAdd.py
from step3.util import *

def test():
    enter('12')
    add('4')
    assertResult('16')
    add('30')
    assertResult('46')
    close()



testAddAndSubtract.py
from step3.util import *

def test():
    enter('12')
    add('4')
    assertResult('16')
    subtract('5')
    assertResult('11')
    close()


Now, you can launch the Marathon IDE again and run them.

Step 4: Automate tests with JUnit

At this stage if we want to run the tests we must start the Marathon IDE and manually run the tests or use the batch mode.
To run all the tests, from step 3, in batch mode execute next command line: marathon -b <some-dir>\tutorial\step3.
But, what if we write some Java unit tests and run them with JUnit? And if we are using automated builds, with tests run, How can we also run the Marathon tests?
Let's write a test suite that uses the scripts written so far. We assume here that the working directory is set to the right Marathon project on startup (see the line with System.getProperty("user.dir")).
TutorialTestSuite.java

import net.sourceforge.marathon.Constants;
import net.sourceforge.marathon.junit.TestCreator;
import net.sourceforge.marathon.component.MarathonNamingStrategy;

import junit.framework.Test;

public class TutorialTestSuite {

    public static Test suite() throws Exception {
	
        String marathonProject = System.getProperty("user.dir");

        System.setProperty(Constants.PROP_RECORDER_NAMINGSTRATEGY, MarathonNamingStrategy.class.getName());
        System.setProperty(Constants.PROP_APPLICATION_PYTHONPATH, marathonProject);
        System.setProperty(Constants.PROP_TEST_DIR, marathonProject);
		
        TestCreator creator = new TestCreator();
        
        return creator.getTest("AllTests");
		
    }

}
After compiling this file, we can run the tests using JUnit.
To run the tests, add next elements to your classpath: Also, add next JVM options:

Download

You can download the tutorial here.
What's next?

Follow next link for the tutorial part 2.



1This should work on Windows with a JRE / JDK 1.2 or later; alternatively use the command line: java -jar calculator.jar or java -cp calculator.jar calculator.CalculatorDialog

2A cachedir directory was created by Marathon while you ran the recorder in the marathon installation directory.

3You can either record them or write the scripts by hand



Annex

Create a new Marathon project
Start the Marathon IDE (using the <marathon-dir>\marathon.bat file):

Click the "New" button to create a new project.
Select a directory where you want to create the project and name it "Step 1".
In the "Main" tab type the main class name: calculator.CalculatorDialog.
In the "Class path" tab click the "Add Archives" button and point to the calculator jar file.
Click the "Save" button, you're back to the previous window.
Now, click "OK". Next window should open:

Check you can run the application, click on the "Open Application" button in the toolbar (the black running man icon). You should see the calculator image. Next to the "Open Application" is the "Close Application" button to stop it.