Software Testing: Lecture 2 - Clear Testing Goals (Usually) Mean Good Testing
Software Testing: Lecture 2 - Clear Testing Goals (Usually) Mean Good Testing
2
Question – what goals might you have when
testing?
4
Question – How do you know if you’re achieving
those goals?
5
SECTION 2 – SOFTWARE TESTING
METRICS
Definition
Software Metrics
A system or standard of
measurement of software, which
gives a numerical value to some
property of interest.
Exhaustive testing is best
• Exhaustive coverage of possible inputs
– (and input sequences)
• Exhaustive coverage of possible context states
– E.g. operating system state
– E.g. hardware state
– E.g. (for reactive systems) external world state
8
Exhaustive testing is impossible
• But that doesn’t mean it’s not a useful goal
9
Definition
Coverage
A measure of the proportion of a
structure or a domain that a
program, a test case, a test suite
exercises.
What’s a desirable coverage criterion?
Path Coverage
A test suite achieve path coverage if it
results in each path through the program
being taken at least once.
Example
Coverage of the red red path: a:=1
50% b:=a+2
(1/2 paths) If (b>3)
true false
Result:=b Result:=a
b:=a
b:=a
Exercise: minimum of test cases to cover all paths
• No
• Path coverage ensures that each control flow
path has been seen at least once
• But it doesn’t ensure that each path is tried
with all possible variable values
Path coverage is weaker than
exhaustive testing
if (client == TK){
System.out.println(“You may access the secret ST
files”)
} else {
System.out.println(“Sorry “ + client.toString() + “
you may cannot come in here”)
}
15
Path coverage is incredibly expensive
16
Lets scale our ambitions right down
Statement Coverage
A test suite achieves statement coverage if
it results in each statement in the program
being run at least once
17
Statement coverage is very weak
int abs(int x) {
x<0 ans = x;
if (x < 0)
ans = -x;
x>=0
return ans;
}
Statement coverage is very weak
int abs(int x) {
x<0 ans = x;
if (x < 1)
ans = -x;
x>=0
return ans;
}
int abs(int x) {
x<0 ans = -x;
if (x < 1)
ans = -x;
x>=0
return ans;
}
Branch Coverage
A test suite achieves branch coverage if it
results in each branch in the program being
taken at least once
21
Branch Coverage Example
int abs(int x) {
x<0 int ans = x;
if (x < 1)
ans = -x;
x>=0
return ans;
}
Branch coverage often beats statement
coverage
int abs(int x) {
x<0 int ans = x;
if (x < 1)
ans = -x;
x>=0
return ans;
}
int abs(int x) {
x<0
int ans = x;
if (x < 1)
ans = -x;
x>=0 return ans;
}
int abs(int x) {
x<0
int ans = -x;
if (x < 1)
ans = -x;
x>=0 return ans;
}
• No
27
Quick Quiz
1. Draw the CFG for this code int function(int x){
33
A problem with branch coverage
if (averageMark >= 50 OR hardFailCount == 0){
grade = "pass";
} else {
grade = "fail";
}
34
Condition Coverage
• Each time one has a branching instruction (if,
for, while…) that contains one or several
conditions, each condition’s outcome (True or
False) is a possibility
• Condition coverage is the percentage of these
possibilities that were executed by a test suite.
if (a || b)
36
Modified Condition/Decision Coverage
• Consists of:
– 100% branch coverage
– 100% condition coverage
– Each entry/exit point is exercised
– Each condition affects the behaviour
independently
if (a || b)
38
Notes on MC/DC
• Significant because DO-178B mandates it for
avionics software
• Amman & Offutt book calls this ACC (with
variants GACC, CACC and RACC)
– Helpfully, neither “MC/DC” nor “ACC” appears in
their index
– Section 3.2.1 covers this (bottom of p108 in my
version)
39
• Any questions?
40
SECTION 3 – METRIC
SUBSUMPTION
Quick Quiz – An Answer
X<=10 X>10 int function(int x){
int y = 0;
if (x > 10)
y = 1;
else
X>5
y = 2;
X<=5 if (x > 5)
y = y*2;
if (x > 0)
X>0 y = y*3;
return y;
X<=0
}
48
SECTION 4 – METRICS AND
METHOD EVALUATION
Question – You’ve evaluated your testing
on a project, and you’ve got good coverage.
Does that mean that the approach you
used is a good method for all projects?
• No!
• Different projects will have different testing
requirements
• Rigorous evaluation requires confidence over
multiple projects
SECTION 5 – SOME METRICS AREN’T DIRECTLY
ABOUT TESTING, BUT HAVE IMPLICATIONS FOR
TESTING
So, you want to know how much testing you
have to do, and how much it will cost you…
57
Cyclomatic Complexity
• The metric measures the number of possible paths in a
program (in a subroutine)
• How to count it:
– M=E-N+2P
(M= Cyclomatic Complexity, E number of edges in control-flow
graph, N number of nodes of the graph, P number of entries)
– M = 1+ number of if, from…until, select, for, while…
• Pitfalls:
– Difficult to understand for non-specialists
– What does it really measure?
Example
a:=1
E=5, N=5, P=1
b:=a+2
M=2
b>3
yes no
Result:=b Result:=a
b:=a
b:=a
Number of distinct states
• The metric measures the number of states in a
model of the program
• Pitfalls:
– Model generally non-trivial to make
Example
open closed
Distinct states: 2
Interdependence
• The metric measures the number of
interdependences between two modules
• How to count it:
– Count all direct references in one class to another
• Pitfalls:
– How to count dependencies that use inheritance?
– Are direct calls to be used?
Inheritance Metrics
• These metrics measures how much classes
inherit one from another.
• How to count them:
– Measure the number of parents or descendants
• Pitfalls:
– How does that relate to the quality of the code?
Inheritance Example: EiffelBase
Once you’ve got a test process in place,
you might want to measure how well
your development is going
65
Bugs per line of code
• The metric measures the number of bugs
found per line of code
• How to count it:
– Count the number of bugs divide it by the number
of lines of code
• Pitfalls:
– What if the testing is weak?
– Can you compare results for two programs?
Examples
• Firefox 1.5: .000077 (locs: 2’172’520, open
bugs: 169)
• Apache Http 2: .00065 (locs: 89000, open
bugs: 58)
• Any questions?
68
SECTION 6 – A USEFUL SUMMARY
OF SMAT SO FAR
Definitions (Dynamic) Testing Techniques
Testing Exploratory testing
Fault, Error and Failure Specification-based testing
Model-based testing
Fuzz testing
Classes of Testing Partition testing
Dynamic / static Boundary testing
Black / white / grey –box Coverage-based testing
Functional / non-functional Fault injection
Now
By email
(I’ll answer in next lecture)
71