Now that we saw this overview of white-box testing, I'd like
to start talking about specific coverage criterion. And I'm going to start
with the first one, which is Statement Coverage. This criterion is
going to be characterized by two aspects, the first one is which
are the Test requirements for the criteria and the second one
is how we measure Coverage for that criteria. In the case
of statement coverage, these test requirements are all the statements in
the program. So this is the basic, the first the, the simplest
coverage criteria in the white-box arena. Let me remind you the assumption
that we made at the beginning. White-box testing is based on the assumption
that if there isn't a faulty element in the code, we need
to exercise it. We need to execute it, in order to find the
fault. And that's exactly what statement coverage does. If there is a
statement that is faulty in the code, we need to exercise it, in
order to find the fault. And therefore, a good measure of how well
we exercise the code, is the ratio of the number of executed statements.
So all the statements that my test cases executed, to the total
number of statements in the program. The higher this number, the better
I exercise my code. And we can also look at coverage criterion
in terms of questions. So what is the questions they were trying
to answer when we look at a specific set of test cases
and we assess the statement coverage that they achieved. And the question
is whether each statement in the program has been executed. So, statement
coverage is satisfied when all the statements in the program have been executed.
And we can satisfy to different degrees and the degrees to which it's
satisfied is measured by this value. So now let's go ahead and measure
statement coverage on our printSum example. What I'm going to show down here is
this progress bar in which we show the amount of coverage, the percentage of
coverage achieved. So what this means is that the, if I get to
this point I've covered 25% of the statements in the code. And my goal
is to get up here to cover all the statements in the code.
We have two test cases for this code. The first one that we just
saw, consists of the inputs a equal to 3 and b equal
to 9, and the second one has the inputs a is equal to
minus 5 and b is equal to minus 8. So now let's see
what happens when we run this test case. When we run this test
case, I'm going to show you by highlighting in the code the parts that
we cover when we start executing the code. We cover the first statement,
then we always execute the second
statement, which computes the result, we continue
the execution, we get to the if statement. If the result is greater
than zero, in this case our result is 12 because we
are working with the inputs 3 and 9, and therefore we
execute the true part of the if, we execute the statement.
And at this point, we just jump to the end. Because we
do not execute the else part of the statement, since we
have executed a true one, and therefore, we cover this final
statement. So at the end of the execution of this test
case, we cover one, two, three, four, five statement out of seven
which is roughly speaking 71%. So we can mark in here
that we more or less got to 71% of coverage for
this code. Now let's look at what happens when we execute
test case number two. In this case again, we execute the
first statement, the second statement, the third statement. In this case
though, the first statement, when it evaluates the value of result,
it sees that the result is not greater than zero because
our inputs are minus five and minus eight. Therefore, you will execute
line number five. And because the result is less than
zero, you will also execute line number six. So, at
this point, all of the statements in our code are
executed and therefore, we achieved a 100% statement coverage, which
was our goal. Before looking at other kinds of coverage,
let's see how our statement coverage is used in practice.
First of all, statement coverage is the most used kind
of coverage criterion in industry. Normally for company that uses
statement coverage, the typical coverage target is 80-90%, which
mean the outcome of the test should be such
that 80-90% of the statements are exercised at the
end of testing. So at this point, you might
be wondering, why don't we just shoot for 100%?
Why don't we try to cover all of the
code? We just saw that we could do it.
And so I'm going to ask you the same question.