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: |
alef'1 |
||||||
![]() |
|||||||
|
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.
|
|||||||
![]() |
|||||||
|
save it as a file named testAdd.py, you should see somthing like: |
|||||||
![]() |
|||||||
|
write the test
|
|||||||
if window('Calculator Example'):
click('1')
click('2')
click('+')
click('4')
click('=')
close(
|
|||||||
|
press control right-click in the the text area, to open the contextual menu, |
|||||||
![]() |
|||||||
|
|||||||
if window('Calculator Example'):
click('1')
click('2')
click('+')
click('4')
click('=')
assert_p('TextField', 'Text', '16')
close(
|
|||||||
|
|||||||
![]() |
|||||||
|
|||||||
![]() |
|||||||
|
|||||||
if window('Calculator Example'):
click('1')
click('2')
click('-')
click('4')
click('=')
assert_p('TextField', 'Text', '8')
close(
|
|||||||
|
and |
|||||||
if window('Calculator Example'):
click('1')
click('2')
click('-')
click('4')
click('=')
click('C')
assert_p('TextField', 'Text', '0')
close(
|
|||||||
|
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: |
|||||||
enter('12')
add('4')
assertResult('16')
close() |
|||||||
instead of |
|||||||
window('Calculator Example')
click('1')
click('2')
click('+')
click('4')
click('=')
assertText('JTextField', '16')
close() |
|||||||
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: |
|||||||
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:
|
|||||||
from Modules.util import * useFixture(default) def test():
enter('12')
add('4')
assertResult('16')
close()
|
|||||||
|
|||||||
from Modules.util import * useFixture(default) def test():
enter('12')
subtract('4')
assertResult('8')
close()
|
|||||||
|
|||||||
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.
|
|||||||
from step3.util import * def test():
enter('12')
add('4')
assertResult('16')
add('30')
assertResult('46')
close()
|
|||||||
|
|||||||
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")).
|
|||||||
import net.sourceforge.marathon.Constants; import net.sourceforge.marathon.junit.TestCreator; import net.sourceforge.marathon.component.MarathonNamingStrategy;br> 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: DownloadYou can download the tutorial here. writeContactMe() ContactDrop me a mail, start the mail subject with [jlaz]. |
|||||||
|
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 |
|||||||
AnnexCreate 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. |
|||||||