Title: XP Practice: TestFirst Development
1XP Practice Test-First Development
- Tuba Yavuz-Kahveci
- CEN 3031
- University of Florida
2Outline
- What is Test-First Development?
- User Stories
- Test Planning
- Implementing the Test
- JUnit Framework
- Implementing the Unit
3Test-First Development
- Traditional approach code first and then test
- may delay finding bugs
- Test-First design the test cases, implement the
test, and finally do the coding - Code enough to make the tests run successfully
4Test-First Development
- Advantages
- Refactor boldly and mercilessly
- Tests are rerun to see if the improved code still
behaves the same way - Supports collective code ownership
- Immediate feedback if a bug is introduced by
another developer - Quick and accurate integration
- Quick diagnosis of the source of any
incompatibility
5Outline
- What is Test-First Development?
- User Stories
- Test Planning
- Implementing the Test
- JUnit Framework
- Implementing the Unit
6User Stories
- A user story is a short description of the system
behavior for a particular feature - Medium of communication between the customer and
the developer - Small stories
- frequent releases
- easier to make an estimate
-
7Example User Story
A customer can rent a movie for a period of time.
The customer gains some frequent renter points
depending on the type of the movie and the
rental period. The program prints the statement
for each rental and it should include the title
of the movies along with the individual prices,
the total amount owned, and the earned frequent
renter points.
8Possible Questions Answers
- Q What are the movie types?
- A regular, childrens, new release
- Q How is the price calculated?
- A
9Possible Questions Answers
- Q How is the frequent renter points calculated?
- A
- Q Is there any history keeping for the frequent
renter points for each customer? - A No.
10Possible Questions Answers
- How does a typical statement look like?
Statement for Customer_Name Movie_Title_1 Individu
al_Price_1 Individual_Freq_Rent_Points_1 Movie_Tit
le_1 Individual_Price_2 Individual_Freq_Rent_Point
s_2 Movie_Title_1 Individual_Price_N Individual_
Freq_Rent_Points_N Amount owed is
Total_price You earned Total_Freq_Rent_Points
frequent renter points
11Outline
- What is Test-First Development?
- User Stories
- Test Planning
- Implementing the Test
- JUnit Framework
- Implementing the Unit
12User Story ? Tasks
- Divide the implementation of the user story into
tasks - A high-level description of how the computation
will be done
13Example Task
Implementing generation of the rental
statement Keep the title and the type of each
movie. Keep the list of rented movies for each
customer. For a given customer print the
statement Calculate the individual price and
the individual frequent renter points for each
rented movie, the total price and the frequent
renter points earned and print them as the
statement.
14Test Cases for the Task
- We dont have the code that implements the task
yet! - Black-box testing
- Identify the inputs and the outputs and their
domains, i.e., the possible values they can take
15Inputs and Outputs
- Customer Name
- A set of movie rentals
- Title of the movie
- Type of the movie
- Rental period
- Statement
16Input Domains
- Customer Name
- Valid Two non-empty strings (first and last
names) - Invalid Any of the two an empty string
- A set of movie rentals
- Valid non-empty set of movie rentals
- Invalid empty set of movie rentals
17Input Domains
- Movie rental
- Title of the movie
- Valid non-empty string
- Invalid empty string
- Type of the movie
- Valid regular, childrens, new release
- Invalid other than regular, childrens, new
release - Rental Period
- Valid one or more days
- Invalid zero or less days
18Choosing Test Data
- We cannot test our program with every possible
value in the input domain! - Need some good representative values
- Solution Partition the input domains into
equivalence classes based on the specification - Input values that produce similar output should
be in the same equivalence class
19Movie Rentalsr regular, c childrens, n new
release
- One rental r, c, n
- Two rentals r, c, n ? r, c, n
- All the same or all different
- Three rentals r, c, n ? r, c, n ? r, c, n
- All the same, two of them the same, all different
- Four rentals
- r, c, n ? r, c, n ? r, c, n ? r, c, n
- All the same, three of them the same, two of them
the same, all different - More than four rentals
20Rental Period (d)
- Regular
- d2 price2
- dgt2 price2(d-2)1.5
- Frequent renter points 1
- Independent of d
- Valid 1, 2, 3, 4, 5,
- Singletons for boundary value, boundary value-1,
boundary value1 - Invalid -1, -2, , 0
21Rental Period (d)
- Childrens
- d3 price1.5
- dgt3 price1.5(d-3)1.5
- Frequent renter points 1
- Independent of d
- Valid 1, 2, 3, 4, 5, 6,
- Singletons for boundary value, boundary value-1,
boundary value1 - Invalid -1, -2, , 0
22Rental Period (d)
- New release
- price3d
- d1 Frequent renter points 1
- dgt1 Frequent renter points 2
- Valid 1, 2, 3, 4,
- Singletons for boundary value, boundary value-1,
boundary value1 - Invalid -1, -2, , 0
23Outline
- What is Test-First Development?
- User Stories
- Test Planning
- Implementing the Test
- JUnit Framework
- Implementing the Unit
24Implementing Tests
- Manual testing
- Run the program with test data
- Compare the expected output with the actual
output in a nonsystematic (manual) way - Costly to repeat
- Automated testing
- Implement the comparison of the expected output
with the actual output once - Do the testing by running the testing code as
many times as needed!
25JUnit
- An automated testing framework
- Specialized classes for creating and
manipulating - Test cases TestCase class
- For implementing a test case given an input
check if the output is equivalent to what is
expected? - Test suites TestSuite class
- For implementing a test suite does the program
runs correctly for a different set of test cases? - Test success failure Assert class
26- public class IntegerArithmetic
- public int add(int op1, int op2)
- public int subtract(int op1, int op2)
- public int multiply(int op1, int op2)
- public int divide(int op1, int op2)
- public int minus(int op)
import junit.framework. public class
testIntegerArithmetic extends TestCase
private IntegerArithmetic _calculator
private int _op1 private int _op2
public testIntegerArithmetic(String s)
super(s) protected void setUp()
_calculator new IntegerArithmetic()
_op1 5 _op2 6 public void
testAdd() Assert.assertEquals(_op1_op2,
_calculator.add(_op1,_op2))
// continued
Class to be tested
Test Class
27- public class IntegerArithmetic
- public int add(int op1, int op2)
- public int subtract(int op1, int op2)
- public int multiply(int op1, int op2)
- public int divide(int op1, int op2)
- public int minus(int op)
// continued
public static void main(String
args) junit.textui.TestRunner.run(new
testIntegerArithmetic("testAdd"
)) junit.textui.TestRunner.run(
testIntegerArithmetic.class)
junit.textui.TestRunner.run(suite())
public static Test suite()
TestSuite suite new TestSuite()
suite.addTest(new testIntegerArithmet
ic("testAdd")) suite.addTest(new
testIntegerArithmetic("test for mult..")
public void runTest()
testMultiply() )
suite.addTest(new testIntegerArithmetic(
"testSubtract")) return suite
Class to be tested
Test Class
28 java junit.swingui.TestRunner
29After running the tests
30After running the tests
After implementing the subtraction as if it was
addition
31More Information
- Readins in Agile Development Methods Extreme
Programming (textbook) - Chapter 34 Building Tests
- http//www.junit.org
- http//junit.sourceforge.net/doc/testinfected/test
ing.htm - http//junit.sourceforge.net/doc/cookbook/cookbook
.htm
32Outline
- What is Test-First Development?
- User Stories
- Test Planning
- Implementing the Test
- JUnit Framework
- Implementing the Unit
33Task
Implementing generation of the rental
statement Keep the title and the type of each
movie. Keep the list of rented movies for each
customer. For a given customer print the
statement Calculate the individual price and
the individual frequent renter points for each
rented movie, the total price and the frequent
renter points earned and print them as the
statement.
34A Quick Design
35testCustomer class
- Will be implemented using JUnit framework
- Extends testCase class
- A method for each test case
- Will make calls to
- Constructors of Customer, Movie, and Rental
classes - new Customer(joe)
- new Rental(new Movie(Troy,1),3)
- addRental and statement methods of Customer class
36testCustomer class
- Assignment! Your first group project task
- Due January 28th
- http//www.cise.ufl.edu/class/cen3031sp05/project.
html - You will learn JUnit and get to know your group
members