Integration Testing Reading:: The Art of Unit Testing, Ch. 1, 3, 4-5 (Osherove) Code Complete, Ch. 29 (Mcconnell)
Integration Testing Reading:: The Art of Unit Testing, Ch. 1, 3, 4-5 (Osherove) Code Complete, Ch. 29 (Mcconnell)
Lecture 14
Integration Testing
Reading:
The Art of Unit Testing, Ch. 1, 3, 4-5 (Osherove)
Code Complete, Ch. 29 (McConnell)
slides created by Marty Stepp
http://www.cs.washington.edu/403/
Integration
integration: Combining 2 or more software units
often a subset of the overall project (!= system testing)
cascade of interdependencies
cannot find and solve problems one-at-a-time
2
Phased integration
phased ("big-bang") integration:
design, code, test, debug each class/unit/subsystem
separately
combine them all
pray
Incremental integration
incremental integration:
develop a functional "skeleton" system (i.e. ZFR)
design, code, test, debug a small new piece
integrate this piece with the skeleton
test/debug it before adding any other pieces
Benefits of incremental
Benefits:
Errors easier to isolate, find, fix
reduces developer bug-fixing load
Drawbacks:
May need to create "stub" versions of some features that
have not yet been integrated
Top-down integration
top-down integration:
Start with outer UI layers and work inward
must write (lots of) stub lower layers for UI to interact
with
allows postponing tough design/debugging decisions
(bad?)
Bottom-up integration
bottom-up integration:
Start with low-level data/logic layers and work outward
must write test drivers to run these layers
won't discover high-level / UI design flaws until late
"Sandwich" integration
"sandwich" integration:
Connect top-level UI with crucial bottom-level classes
add middle layers later as needed
more practical than top-down or bottom-up?
Daily builds
daily build: Compile working executable on a daily basis
continuous integration:
Adding new units immediately as they are written.
9
Integration testing
integration testing: Verifying software quality by
testing two or more dependent software modules as a
group.
challenges:
Combined units can fail
in more places and in more
complicated ways.
How to test a partial system
where not all parts exist?
How to "rig" the behavior of
unit A so as to produce a
given behavior from unit B?
10
Stubs
stub: A controllable replacement for an existing
software unit to which your code under test has a
dependency.
useful for simulating difficult-to-control elements:
network / internet
database
time/date-sensitive code
files
threads
memory
12
13
14
Injecting a stub
seams: Places to inject the stub so Class A will talk to it.
at construction(not ideal)
A aardvark = new A(new StubB());
through a getter/setter method
(better)
You should not have to change A's code everywhere (beyond using
your interface) in order to use your Stub B. (a "testable design")
15
"Mock" objects
mock object: A fake object that decides whether a
unit test has passed or failed by watching interactions
between objects.
useful for interaction testing (as opposed to state
testing)
16
@RunWith(JMock.class)
public class ClassATest {
private Mockery mockery = new JUnit4Mockery();
// initialize jMock
attach to mock
19
jMock API
jMock has a strange API based on "Hamcrest" testing
syntax.
Specifying objects and calls:
oneOf(mock), exactly(count).of(mock),
atLeast(count).of(mock), atMost(count).of(mock),
between(min, max).of(mock)
allowing(mock), never(mock)
The above accept a mock object and return a descriptor that you
can call methods on, as a way of saying that you demand that
those methods be called by the class under test.
atLeast(3).of(mockB).method1();
"I expect that method1 will be called on mockB 3 times here."
20
Expected actions
.will(action)
actions: returnValue(v), throwException(e)
values:
equal(value), same(value), any(type),
aNull(type), aNonNull(type), not(value),
anyOf(value1, ..,valueN)
oneOf(mockB).method1();
will(returnValue(anyOf(1, 4, -3)));
"I expect that method1 will be called on mockB once here, and
that it will return either 1, 4, or -3."
21
Using stubs/mocks
together
Suppose a log analyzer reads from a web service.
If the web fails to log an error, the analyzer must send
email.
How to test to ensure that this behavior is occurring?
22