Test Driven Development and JUnit

nutmegactΛογισμικό & κατασκευή λογ/κού

11 Νοε 2012 (πριν από 4 χρόνια και 11 μήνες)

250 εμφανίσεις

Lecture objectives


Learn about testing techniques and how to apply
these to optimize code development


Develop code starting from a test case (test driven
development)


Debug using eclipse


Develop a painting program using event
-
oriented
programming

CS340

1

Testing

CS340

2

Testing


A

program or part of a program under

controlled
conditions

to verify that results are as expected


D
etects program defects after the program
compiles.


C
annot detect all possible defects in complex
programs


Testing is an art and science


CS340

3

Testing Levels


Unit testing
: tests the smallest testable piece


Integration testing
: tests integration among units


System testing
: tests the whole program


Acceptance testing
: system testing to show program
meets functional requirements


CS340

4

Types of Testing


Black
-
box testing:


tests the item based on its interfaces and functional
requirements


is also called
closed
-
box

or
functional

testing


Is accomplished by


varying input parameters


comparing with independently calculated results

CS340

5

Types of Testing
(cont.)


White
-
box testing:


tests the item with knowledge of its internal structure


is also called
glass
-
box, open
-
box,

or
coverage

testing


exercises as many paths through the element as
possible


provides appropriate coverage


statement


ensures each statement is executed at least once


branch


ensures each choice of branch (if , switch, loops) is
taken


path


tests each path through a method



CS340

6

Preparations for Testing


E
arly in the design stage!!


Easier to find


Less expensive it is to correct it


T
est plans include deciding:


how the software will be tested


when the tests will occur


who will do the testing


what test data will be used


CS340

7

Testing Tips for Program Systems

1.
DOCUMENTATION! (use
javadoc
)

2.
T
race of execution: display the method name as
you enter it

3.
Display

1.
All input parameters
upon
entering
a
method

2.
V
alues of any class attributes accessed by the method

3.
A
ll method outputs after returning from a method

4.
Class attributes that are modified by a method

CS340

8

Testing Tips for Program Systems
(cont.)


An efficient way to display values of parameters, return
values, and class attributes:


private static final boolean TESTING = true; //
or false to


//
disable


if (TESTING) {



// Code for output statements

}



CS340

9

Developing the Test Data


In black
-
box testing, check for


all expected inputs


unanticipated data


In white
-
box testing


ensure all combinations of paths through the code are
executed

CS340

10

Testing Boundary Conditions



Example

for (int i = 0; i < x.length; i++) {



if (x[i] == target)



return i;

}


Test the boundary conditions (for white
-
box and black
-
box testing) when
target

is:


first element (
x[0] == target

is
true
)


last
element
(
x[length
-
1]
== target

is
true
)


not in array
(
x[i]
== target

is always
false
)


present multiple times
(
x[i] == target

for more than one
value of
i
)



CS340

11

Testing Boundary Conditions
(cont.)

for (int i = 0; i < x.length; i++) {



if (x[i] == target)



return i;

}


Test for the typical situation when
target

is:


somewhere in the middle


and for the boundary conditions
when
the array has


only one element


no elements



CS340

12

Stubs


Stubs

are method placeholders for methods called
by other classes, but not yet implemented


A sample stub:

public void save() {


System.out.println
("Stub for save has been
called");


modified = false;

}


CS340

13

Preconditions and Postconditions


P
recondition
: statement of any assumptions or constraints on
the input parameters before a method begins execution


P
ostcondition
:

result of executing the method, including any
change to the object’s state


/** Method Save



pre: the initial directory contents are read
from a data file



post: writes the directory contents back to a
data file

*/

public void save() {

. . .

}


CS340

14

Drivers


Another testing tool


A driver program


declares any necessary object instances and variables


assigns values to any of the method's inputs (specified
by the preconditions)


calls the method


displays the outputs returned by the method

CS340

15

Finally


JUnit
, a popular program for Java projects, helps
you develop testing programs


Many IDEs are shipped with debugger programs
you can use for testing

CS340

16

Test Driven Development and
JUnit

CS340

17

Program testing


Testing is a
science






A

test framework facilitates:


Writing test cases


Organizing Test suites


Running test suites


CS340

18

JUnit


Testing framework used in Eclipse and other IDEs
(
NetBeans
)


Test driven development:

CS340

19

JUnit


Example with eclipse

CS340

20

Debugging

CS340

21

Debugging code


It used to be:





Today it is:

CS340

22

Debugger


Helps stop our program


Look into variable values


Why use it?


Debug!


Understand other people’s code


Understand OOP

CS340

23

… and Java Swing!

Event
-
Oriented Programming

24

CS340

Components and Events


Component
: represents the objects displayed on
screen that can interact with the user


Ex. button


Event: something that happens and triggers a
component reaction


Ex. Mouse click

CS340

25

Event Listeners


Registered for a specific event


Action listener: called when an action event occurs


j
ava.awt.event.*

Interface
ActionListener

extends
EventListener

{


void
actionPerformed
(
ActionEvent

e)

}


You always need to register an
EventListener


CS340

26

Event
-
oriented programming


Example using Eclipse

CS340

27