1.
CODE COVERAGE TESTING
1. AIM
To study and implement the various types of code coverage testing using automated tools in
java environment.
CODE COVERAGE / TEST COVERAGE
Code coverage analysis is used to measure the quality of software testing, usually using
dynamic execution flow analysis.
TYPES OF CODE COVERAGE
1. Statement Coverage (Line Coverage)
Check, whether each statement or line in the program has executed or not
2. Function Coverage
Check, whether each method or function or class in the program has executed or
not
3. Branch Coverage / Decision Coverage
Check, whether each branch of each control structure (like if and case
statements) in the program has executed or not
It does not focus the Boolean condition in control structure
4. Condition Coverage / Predicate Coverage
Check, whether each Boolean sub expression evaluated both to true or false in
the program
It mainly focus the Boolean condition in control structure
I. STATEMENT COVERAGE
It checks whether each statement or line in the program has executed or not
It includes
Expression statement, declaration statements
Return statement, control flow statements (e.g. if, if-else, for, while)
Break, Continue Statement
1
Formula
SC= Number of statements exercised
X 100
Total number of statements
Drawbacks
It does not execute false statements
2
I. EXAMPLE OF STATEMENT COVERAGE TESTING
(TESTING STUDENT INFORMATION)
USED IDE: ECLIPSE KEPLER IDE
1. PROJECT CREATION IN ECLIPSE IDE
3
2. PROJECT SETTINGS
4
3. PROJECT TREE
5
4. CREATING A NEW CLASS IN ECLIPSE IDE
6
5. SOURCE CODE
(StudentInfo.java)
import java.io.*;
public class StudentInfo {
DataInputStream ds=new DataInputStream(System.in);
String name;
int id;
String dept;
public void input()throws Exception
System.out.print("Enter Student Name: ");
name=ds.readLine();
System.out.print("Enter Student Id: ");
id=Integer.parseInt(ds.readLine());
System.out.print("Enter Student Dept: ");
dept=ds.readLine();
public void result()throws Exception
input();
System.out.println("-----------------------------");
System.out.println("\tStudent Details");
System.out.println("-----------------------------");
System.out.println("Name\t:"+name);
System.out.println("Id\t:"+id);
System.out.println("Dept\t:"+dept);
7
6. ADDING JUNIT TESTCASE TO EXISTING CLASS
8
7. SET THE NEW NAME TO JUNIT TEST CASE
9
8. METHODS SELECTION TO JUNIT TESTING
10
9. ADDING JUNIT 4
TEST CLASS
(StudentInfoTest.java)
import static org.junit.Assert.*;
import org.junit.Test;
public class StudentInfoTest {
// create an object for existing class
StudentInfo obj=new StudentInfo();
@Test
public void testResult()throws Exception {
// call loop coverage via JUNIT
obj.result();
11
10. RUNNING TEST CLASS
12
11. GENERAL OUTPUT
13
12. JUNIT TEST RESULT
14
13. RESULT
13.1 SELECT PROJECT PROPERTIES IN ECLIPSE IDE
15
14. COVERAGE RESULT
14.1 RESULT (A)
16
14.2 RESULT (B): OVERALL PROJECT COVERAGE RESULT
17