Professional Documents
Culture Documents
CSCE 5430
Unit Testing
Testing Levels
1
10/8/21
Testing Levels
n Unit testing
¨ Individual program units,
such as procedure,
methods in isolation
n Integration testing
¨ Modules are assembled to
construct larger
subsystem and tested
n System testing
¨ Includes wide spectrum of
testing such as
functionality, and load
Development and testing phases in
n Acceptance testing
¨ Customer’s expectations V model
from the system
What Is an Unit?
n Subprogram or script
n Java class method
2
10/8/21
Unit Testing
Slide from
3
10/8/21
Unit testing
n Scaffolding needed to create the environment in
which the module should be tested
¨ stubs
n Modules used by the unit under test
n It is a dummy program
¨ drivers
n Program activating the unit under test
n It provides input data to unit under test and reports the
test result
Unit Testing
4
10/8/21
10
5
10/8/21
11
12
6
10/8/21
Pair programming
¨ In XP code is being developed by two programmers working side
by side
¨ One person develops the code tactically and the other one inspects
it methodically by keeping in mind the story they are
implementing
13
Test-first process in XP
14
7
10/8/21
n References
• http://www.junit.org
• JUnit Cookbook
• Test Infected - Programmers Love Writing Tests
• JUnit – A cooks tour
• Java Extreme Programming Cookbook, chapter 4, Eric Burke and Brian
Coyner, O’REILLY
15
The Problem
n JUnit Test Infected: Programmers Love Writing Tests
“Every programmer knows they should write tests for their
code. Few do. The universal response to “Why not?” is
“I’m in too much of a hurry.” This quickly becomes a
vicious cycle – the more pressure you feel, the fewer tests
you write. The fewer tests you write, the less productive
you are and the less stable your code becomes. The less
productive and accurate you are, the more pressure you
feel.”
16
8
10/8/21
17
18
9
10/8/21
Example: Money.java
class Money{
private int fAmount;
private String fCurrency;
19
20
10
10/8/21
MoneyTest.java
import junit.framework.*;
21
22
11
10/8/21
23
assertTrue(!m12USD.equals(null));
assertEquals(m12USD, m12USD);
assertEquals(m12USD, new Money(12, “USD”));
assertTrue(!m12USD.equals(m14USD));
}
24
12
10/8/21
Duplicated Code
public void testSimpleAdd(){
Money m12USD = new Money(12, “USD");
Money m14USD = new Money(14, “USD");
Money expected = new Money(26, “USD");
Money result = m12USD.add(m14UDS);
assertEquals(expected.amount(), result.amount());
}
assertTrue(!m12USD.equals(null));
assertEquals(m12USD, m12USD);
assertEquals(m12USD, new Money(12, “USD”));
assertTrue(!m12USD.equals(m14USD));
}
25
A setUp method
n A common fixture for running the tests
26
13
10/8/21
assertEquals(expected.amount(), result.amount());
}
assertTrue(!m12USD.equals(null));
assertEquals(m12USD, m12USD);
assertEquals(m12USD, new Money(12, “USD”));
assertTrue(!m12USD.equals(m14USD));
}
27
tearDown method
n Cleanup code
n Release any permanent resources you allocated in setUp
n Often ignore the tearDown() method
n Useful if your tests do things like open database
connections, show GUI frames, …
public void tearDown(){
this.game.shutdown();
}
28
14
10/8/21
29
30
15
10/8/21
31
32
16
10/8/21
….
33
Test Suite
…
…
… …
…
test methods
34
17