Calculator example.A calculator application was developed and the result is bundled in a jar file ( |
alef'1 |
||||
![]() |
|||||
Step 1: Write some acceptance tests
start-IDE.bat file): |
|||||
![]() |
|||||
create a new empty test (a test is a Python script file)
write the test
|
|||||
window('Calculator Example') click('1') click('2') click('+') click('4') click('=') close() |
|||||
right-click (on MS-Window systems) in the the text area, to open the contextual menu , |
|||||
![]() |
|||||
select assert text='16' stop recording (the blue rectangle button), the script now looks like: |
|||||
window('Calculator Example') click('1') click('2') click('+') click('4') click('=') assertText('JTextField', '16') close() |
|||||
replay the test (click on the green arrow). Change the asserted value to some bad value and see the error report you can create two more tests: testSubtract and testClear3 |
|||||
window('Calculator Example') click('1') click('2') click('-') click('4') click('=') assertText('JTextField', '8') close() |
|||||
and |
|||||
window('Calculator Example') click('1') click('2') click('-') click('4') click('=') click('C') assertText('JTextField', '0') close() |
|||||
Step 2: Refactor the TestsAs 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 * from defaultFixture 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 testing scripts as :
|
|||||
from step2.util import * def test(): enter('12') add('4') assertResult('16') close() |
|||||
|
|||||
from step2.util import * def test(): enter('12') subtract('4') assertResult('8') close() |
|||||
|
|||||
from step2.util import * 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, load the new scripts and run them. Step 4: Automate tests with JUnitAt this stage if we want to run the tests we must start the Marathon IDE and manually open (load) each test script and run it. Marathon smoothly integrates with JUnit (a regression testing framework), so all the tests scripts can automatically be run à la JUnit. Two helper classes, MarathonTestSuite and MarathonTestCase, are provided in the net.sourceforge.marathon.junit package you can use to start the tests scripts, written in Pyhon, from Java. Let's write a test suite that uses the scripts written so far4.
|
|||||
package step4; import junit.framework.Test; import junit.framework.TestSuite; import net.sourceforge.marathon.junit.MarathonTestCase; import java.io.File; import java.io.FileFilter; import java.net.MalformedURLException; public class TutorialTestSuite { public static Test suite() { TestSuite suite = new TestSuite("Tutorial Acceptance Tests"); addTests(suite, "step2"); addTests(suite, "step3"); return suite; } private static void addTests(TestSuite suite, String dir) { File file = new File(dir); File[] files = file.listFiles(new TestScriptFilter()); for (int i = 0; i < files.length; i++) { try { suite.addTest(new MarathonTestCase(files[i].toURL())); } catch(MalformedURLException me) { me.printStackTrace(); // the system has some problem here } } } static class TestScriptFilter implements FileFilter { public boolean accept(File pathname) { return pathname.getName().startsWith("test") && pathname.getName().endsWith(".py"); } } } |
|||||
After compiling this file, in the
DownloadYou can download the tutorial here and the. ContactDrop me a mail, start the mail subject with [jlaz]. 1This should work on Windows with a JRE / JDK 1.2 or later; or use the command line: java -cp calculator.jar calculator.CalculatorDialog 2A cachedir directory was created by Marathon while you ran the recorder 3You can either record them or write the scripts by hand 4The easiest would be to launch JUnit with MarathonTestSuite as starting class, with "-Dtest.name=" option, but as we're using Python modules, without Marathon tests they would fail. |