Unit-5 Software Coding & Testing: .1 Code Review (Coding Concepts)
Unit-5 Software Coding & Testing: .1 Code Review (Coding Concepts)
Unit-5 Software Coding & Testing: .1 Code Review (Coding Concepts)
ERROR
FAULT
BUG
FAILURE
DEFECT
• Error : Error is a kind of mistake. This may be a syntax error or misunderstanding of specifications. Sometimes
it may be logical error.
• Fault : An error may lead to one or more faults. More precisely a fault is the representation of an error.
• Bug : A software bug is an error or fault in a computer program or system that causes it to produce an incorrect
or unexpected result, or to behave in unintended ways.
• Failure : When software is unable to perform as per the requirements, it is called failure. Failure occurs when
a fault executes. This is a demonstration of an error (or defect or bug).
• Defect : Defect is defined as the deviation from the actual and expected result of application or software
Some other testing terms are :
• Test case : Test case is a set of conditions or variables under which a tester will determine whether a system
under test works correctly or satisfy requirements.
This is the triplet [I, S, O], where I is the data input to the system, S is the state of the system at which the data
is input, and O is the expected output of the system.
Software Coding & Testing
• Test suite : This is the set of all test cases with which a given software product is to be tested.
• Testing is four stage process :
Unit testing subsystem testing system testing acceptance testing.
• Initially all the modules are tested individually by their programmers, then interactions between these modules
are tested (integration testing), then whole system as a single component is tested and finally the system is tested
against users' data for final approval from the users.
Difference between verification and validation
Verification Validation
Verification is the process of confirming that Validation is the process of confirming that
software meets its specification. software meets customers' requirements.
Verification is the process of determining Validation is the process of determining
whether the output of one phase of software whether a fully developed system confirms to
development confirms to that of its previous its requirements specification.
phase.
Verification is concerned with phase Validation is concerned with final product be
containment of errors error free.
Verification "are we doing right ?" And Validation "have we done right ?"
Verification comes before validation. Validation comes before verification.
It is static testing. It is dynamic testing.
Cost of verification is less. Cost of validation is more.
Verification does not include the execution of Validation includes the execution of code.
code.
Both strategies are used to finds defects in software project, but in different way, Verification is used to identify the
errors in requirement specifications & Validation is used to find the defects in the implemented software application.
Design of test cases
• We must design an optimal test suite that is of reasonable size and can uncover as many errors existing in the
system as possible.
• Testing a system using a large collection of test cases that are selected at random does not guarantee that all
of the errors in the system will be uncovered.
Example :
int x, y; - Invalid test suit :
take values of x and y {(x=3, y=2), (x=8, y=4), (x=18, y=12)}
from user - Valid test suit:
if (x>y) then {(x=3, y=2), (x=4,y=5)}
x is greater;
else
y is greater;
Software Coding & Testing
• Systematic approach should be followed to design an optimal test suit. Each test case is design to detect
different errors.
• There are mainly two approaches to systematically design test cases :
Black box testing White box testing
• In the black-box testing approach, test cases are designed using only the functional specification of the
software, i.e. without any knowledge of the internal structure of the software.
• So, black-box testing is known as functional testing.
• In the white-box testing approach, designing test cases requires thorough knowledge about the internal
structure of software.
• So, the white-box testing is called structural testing.
Testing in the large vs. testing in the small
• Software products are normally tested first at the individual component (or unit) level. This is referred to as
testing in the small.
• After testing all the components individually, the components are slowly integrated and tested at each level of
integration (integration testing). Finally, the fully integrated system is tested (called system testing).
• Integration and system testing are known as testing in the large.
Levels of testing
• There are three levels of testing :
o Unit testing : it is first level of testing and individual module is tested.
o Integration testing : combine the modules and test their detailed structures.
o System testing : the whole system is tested ignoring the internal structure of the system.
•Functionality of the black box is understood completely in terms of its inputs and output.
Fizzing
Among all of the above, we will discuss only those techniques which are very successful in detecting errors.
Equivalence class partitioning
• In it, the domain of input values (input test data) to a program is partitioned into a finite number of equivalence
classes.
• So the behaviour of the program is similar for every input data belonging to the same equivalence class.
Software Coding & Testing
• The main idea behind defining the equivalence classes is that if one test case in a class detects an error all other
test cases in the class would be expected to find same error.
• To implement this technique two steps are required.
(i) The equivalence classes are identified by taking each input condition and partitioning in into valid and
invalid classes. For example, if an input condition specifies a range of values from 1 to 100, we identify
one valid equivalence class (1 to 100); and two invalid equivalence classes ( < 1 ) and ( >100 ).
(ii) Generate test cases using the equivalence classes identified in the previous step. Now perform testing on
both valid and invalid equivalence classes.
Valid
and System
Invalid inputs used test
• The aim is to choose at least one element from each equivalence class.
• The following are some general guidelines for designing the equivalence classes :
If the input data values to a system can be specified by a range of values, then one valid and two invalid
equivalence classes should be defined.
If the input data assumes values from a set of discrete members of some domain, then one equivalence
class for valid input values and another equivalence class for invalid input values should be defined.
If the input value is Boolean, then one valid and one invalid equivalence classes should be defined.
• For example, for a program that supposes to accept any number between 1 and 99, there are at least four
equivalence classes from input side. Like :
i. Any number between 1 and 99 is valid input.
ii. Any number less than 1 (includes 0 and all negative numbers).
iii. Any number greater than 99.
iv. If it is not a number, it should not be accepted.
Boundary value analysis (BVA)
• Generally errors are occurred at boundary of domains rather than centre of domain.
• This is because test cases closer to boundary have more chance to detect errors.
• For this reason, boundary value analysis technique has been developed.
• In this technique, selection of test cases performed at the edges of the class. Suppose we have an input variable
x with a range from 1 to 100. The boundary values are : 1, 2, 99, 100.
• Consider a program with two input variables x and y. These input variables have specified boundaries as :
a x b
c y d
Software Coding & Testing
• Both the inputs x and y are bounded by two intervals [a,b] and [c,d] respectively. For input x, we may design
test cases with values a and b, just above a and also just below b. for input y, we may have values c and d,
just above c and just below d. These test cases have more chance to detect an error. Like :
y y
d d
c c
x x
a b a b
''Each dot represents a test case and inner rectangle is the input domain.''
• Basic idea of BVA is to use input variables values at their minimum, just above minimum, a nominal value, just
below maximum and at their maximum.
• BVA test cases are obtained by holding the values of all but one variable at their nominal value.
Example :
• The test cases for BVA in a program with two input variables x and y that may have any value from 100 to
300 are:
(200,100), (200,101), (200,200), (200,299), (200,300), (100,200), (101,200), (299,200) and (300,200).
(given in below figure).
y
300
200
100
x
100 200 300
Input domain
200
100
x
100 200 300
Software Coding & Testing
• For this technique, if a program of n variables, then BVA yields 6n + 1 test cases.
Test cases are : (200,99), (200,100), (200,101), (200,200), (200,299), (200,300), (200,301), (99,200),
(100,200), (101,200), (299,200), (300,200), (301,200).
Advantages of black box testing
• It is Efficient for large code segment.
• Tester doesn't need to know the internal structure of the system.
• Tester perception is very simple.
• Programmer and tester are independent of each other.
• Quicker test case development.
Disadvantages of black box testing
• It is inefficient testing.
• Without clear specification test cases are difficult to design.
• Only a selected number of test scenarios are actually performed. As a result, there is only limited coverage.
''Black box testing is also known as close box testing and opaque testing.''
5.3.3 White box testing :
White
Box
Input Output
A A B
B
• If we test program while it is running, it is called dynamic white box testing, and if we test the program without
running it, only by examining and reviewing it then it is called static white box testing.
• Test cases are designed in this method are based on program structure or logic. Example of white box testing
is circuit testing. As in electric circuit testing, the internal structure is checked.
• Test cases generated using white box testing can :
Guarantee that all independent paths within a module have been exercised at least once.
Exercise all decisions whether they are true or false.
Exercise internal data structure of the program.
• The different methods of white box testing are illustrated in below figure.
Statement coverage
Branch coverage
Condition coverage
Mutation testing
Statement coverage :
• The statement coverage is also known as line coverage or segment coverage.
• The principal idea behind the statement coverage strategy is that unless a statement is executed, it is very hard
to determine if an error exists in that statement.
• It aims to design test cases so that every statement in a program is executed at least once.
Software Coding & Testing
A A
C C
B D B D
E E
F F
G G
In above figure, we can see that all the nodes (A to G) have been tested by designing test cases in two different ways.
All nodes are covered, that's why this strategy is called node testing.
Branch coverage :
• In it, test cases are designed to make each branch condition to assume true and false values in turn.
• Branch testing is also known as edge testing as in it, each edge of a program's control flow graph is traversed
at least once.
• Branch testing guarantees statement coverage, so it is stronger testing strategy than statement coverage.
• We can take above example for branch testing as well.
Example :
int compute_gcd(x, y)
int x, y;
{
For this code test case should be:
1 while (x! = y){
{(x=3,y=3), (x=4,y=3), (x=3,y=4)}
2 if (x>y) then
3 x= x - y;
4 else y= y - x;
5 }
6 return x;
}
Software Coding & Testing
A A
C C
B D B D
E E
F F
G G
A A
C C
B D B D
E E
F F
G G
In above figure, we can see that test cases are designed in a way that every edge has been covered at least one.
All the edges are covered that's why this testing strategy is called edge testing. At a particular time the edge which
is examined is shown using bold arrow.
Note : Using statement and branch coverage user generally attains 80-90% code coverage, which is
sufficient.
Condition coverage :
• In this method test cases are designed to make each component of a composite conditional expression to
assume both true and false value.
• For example, consider the following code :
1. READ X, Y
2. IF(X == 0 || Y == 0)
3. PRINT '0'
In this example, there are two conditions ' X = 0 and Y = 0. Now test cases should be designed in a way that
all conditions get TRUE and FALSE.
Test case 1: (X=0, Y=5)
Test case 2: (X=5, Y=0)
• Condition testing is stronger than branch testing.
• For a composite conditional expression, if n components are there, for condition coverage ' 2n test cases are
required.
• In condition coverage, test cases are increasing exponentially with the number of components. Therefore, a
condition coverage based testing technique is practical only if n (the number of conditions) is small.
Path coverage :
• Path testing is used for module or unit testing.
• It requires complete knowledge of the program structure.
• The effectiveness of path testing deteriorates (ûk{íkk ½xðe) as the size of the software under test increases.
Software Coding & Testing
Mutant
Software Mutant
Generation
Survived
Test
Test Suite Execution
Killed
1
1
1
2
2 4
2 3
3
5
Software Coding & Testing
• Using these basic concepts, the CFG of Euclid's GCD computation algorithm can be drawn as shown.
1. while (x != y)
1
2. { if (x > y) then
3. x=x-y
4. else y=y-x 2
5. }
6. return (x) 3 4
6
Figure 4.7 CFG
Path
• A path through a program is a node and edge sequence from the starting node to a terminal node of the control
flow graph of a program. There can be more than one terminal node in a program.
• Path coverage requires the coverage of linearly independent path.
Linearly independent path
• Linearly independent path is defined as a path that has at least one new edge which has not been traversed
before in any other paths.
• If a path has one new node compared to all other linearly independent paths, then the path is also linearly
independent.
• Sub path is not considered as a linearly independent path.
Cyclomatic complexity
• McCabe's cyclomatic complexity defines an upper bound for the number of linearly independent paths through
a program.
• It is very simple to compute.
• Cyclomatic complexity can be calculated with respect to functions, modules, methods or classes within a
program.
• It is practical way to determine maximum number of independent path in the program.
Software Coding & Testing
• Synonyms of black box testing are functional • Synonyms of white box testing are structural
testing, close box testing, data driven testing testing, glass box testing, clear box testing,
and opaque testing. open box testing, logic driven testing.
• No need to know internal structure of the • Internal structure of the system must be
system. known.
• It is concerned with results. • It is concerned with details and internal
workings of the system.
• Performed by end users and also by testers • Normally done by testers and developers.
and developers.
• Granularity is low. • Granularity is low.
• It is least exhaustive and time consuming. • Potentially most exhaustive and time
consuming.
• Not suited for algorithm testing. • It is suited for algorithm testing.
• Example : search engine. • Example : electrical circuit testing.
Software Coding & Testing
Test Plan
Test Design
Description
Test Execution
Test Incident
Test Log
Report
• A test design specification/description to identify the features to be tested and associated tests.
• A test case specification/description to define test cases identified by test design specifications.
• A test procedure specification to specify the steps for executing a set of test cases.
• A test item transmittal report to identify the test items being transmitted for testing.
• A test log' to document the generated results.
• A test incident report to document occurred events during testing process.
• A test summary report to summarize the results of the testing activities and to provide evaluation of these
results.