The Tutorial
www.ddsteps.org
Data Driven Tutorial
Adam Skogman Björn Granvik
We Have a Dream
The Best Laid Plans...
Req. Develop Unit Test Function Test Acc. Test
Live!
Then, Reality Happens...
Req. Develop Unit Test Pre-Test Function Test Acc. Test
Live!
Bugs? Who knew...
Req. Develop Unit Test Pre-Test Function Test Fix Re-Test Fix Re-Test
L
Go Agile!
9 weeks
Develop Req.
3w Req. Unit Test Function Test Develop Unit Test Acc. Test Acc. Test Live!
Live!
Function Test
Develop Req.
Unit Test
Function Test
Acc. Test
Live!
Circle of Life
Tag Function Test Deploy Update Implement Run tests Commit Detect Changes
Unit Test
Build
Our Dream
Continuous Everything Zero Known Defects at Release Known Project Progress Transparent Project Status Use Case Coverage
What's the Problem, Anyway?
Guest Starring: Pet Clinic
www.springframework.org
It’s just Complicated
Add New Pet (OK)
1. 2. 3. 4. 5. 6. 7.
Log in Click “Find Owner” Click “Find Owners” Click “Charlie Brown” Click “Add New Pet” Validate defaults Enter data
1. 2. 3.
“Snoopy” “1950-10-02” “Dog”
ID 42
NAME Snoopy
BIRTH_DATE 1969-01-13
TYPE_ID 1
OWNER_ID 1
8. 9. 10.
Click “Add Pet” Validate owner page Validate database
Why is Function Testing so Hard?
Functions
testAddPet_Ok
testAddPet_Fail
Combinations
testAddPet_Ok_Harvey testAddPet_Ok_Hobbes testAddPet_Ok_Woodstock testAddPet_Ok_Snoopy
testFindOwner_Ok
testFindOwner_Fail testFindVet_Ok
Hello DDSteps!
Yes, it's Open Source!
1. Pull Out the Data
Add New Pet (OK)
1. 2. 3. 4. 5. 6. 7.
Properties
Log in Click “Find Owner” Click “Find Owners” Click “Charlie Brown” Click “Add New Pet” Validate defaults Enter data
1. 2. 3.
Owner Full Name Pet Name Pet Birthdate Pet Type
“Snoopy” “1969-01-13” “Dog”
8. 9. 10.
Click “Add Pet” Validate owner page Validate database
Data reuse
Put it in a Spreadsheet
One File = One JUnit Test Case Header = Property File name 1:1 Class Name
One Row = One Run
Sheet Name = Method Name
Data Driven Test Case
Basic example
Data and Code separation Setting JavaBeans properties
Does not use
Test Steps Fixtures Spring
Example: Compare strings
The Method Under Test
public class StringComparator { public boolean compare(String one, String two) { ... } }
JUnit is Everywhere
JUnit
Eclipse
IntelliJ IDEA
Ant
Maven
Expensive Test Tool
Cruise Control
AntHill
Gump
Continuum
The Old Way
public public public public public public public public ... void void void void void void void void testCompare() {...} testCompareDifferentOrder() {...} testCompareSwedishCharacters() {...} testCompareSmallCharacters() {...} testCompareCaseInsensitive() {...} testCompareStrangeCharacters() {...} testCompareNull() {...} testCompare...() {...}
The Data Driven Way
Strings to compare
Colors, just for our own pleasure.
Delimit Data using Data and "---" Properties using "---"
The Test
public class DdStringComparatorTest extends DdTestCase { private String one; private String two; private boolean result; public void setOne(String one) { … }
public void testCompare() { StringComparator tested = new StringComparator(); boolean actual = tested.compare(one, two) assertEquals(result, actual); } protected DataLoader createDataLoader() { return CachingExcelDataLoader.getInstance(); }
(Backup) Keep the matrix green!
(Backup) The Bug
public class StringComparator { ...
public boolean compare(String one, String two) { return one.equals(two); }
}
Let's fix it!
(Backup) Fixed code
public boolean compare(String one, String two) { if (one == null) { return false; } return one.equalsIgnoreCase(two); }
(Backup) Result
(Backup) ÅÄÖ?
Still fine
Summary
Test Driven Too Easy? Can Do a Lot
Unit Testing Integration Testing
Loads of Opportunities
Testing Web Applications
PetClinic from Above
JWebUnit
HTTP HTTP HTTP API JWebUnit HttpUnit JMeter
Some Tool
TestCase
jwebunit.sourceforge.net httpunit.sourceforge.net
JWebUnit API
Navigate
webBrowser.clickLinkWithText("Find owner"); webBrowser.clickLinkWithImage("...next.png");
Execute
webBrowser.setFormElement("name", "Snoopy"); webBrowser.selectOption("type", "Dog"); webBrowser.checkCheckbox("fictional"); webBrowser.clickButton("Add"); webBrowser.submit("Finish");
Validate
webBrowser.assertTitleEquals("Login Page"); webBrowser.assertFormElementEquals("name","Snoopy"); webBrowser.assertTextInElement( "owner","Charlie Brown");
JWebUnit – Start Here
Code
WebBrowser webBrowser = new WebBrowser(); webBrowser.setUrl(“http://localhost:8080”); webBrowser.beginAt("/"); webBrowser.clickLinkWithText("Find owner");
JWebUnit - Choose a Form
HTML
Code
setWorkingForm(owner); submit();
Data
owner = "Charlie Brown"
JWebUnit – Click "Add New Pet"
Same, same
JWebUnit - Entering Data
HTML