0% found this document useful (0 votes)
27 views11 pages

Understanding Branch Coverage in Testing

Branch coverage checks that all branches in a program's code have been executed, including the true and false branches of if statements, nested if statements, and switch statements. It does not check boolean expressions. Statement coverage alone does not imply branch coverage, as shown by an example program that checks a flag but only executes one branch. The document then provides an example Java program to find prime numbers and the branch coverage results for inputs of a valid prime number, invalid prime number, and invalid data.

Uploaded by

Mallika
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views11 pages

Understanding Branch Coverage in Testing

Branch coverage checks that all branches in a program's code have been executed, including the true and false branches of if statements, nested if statements, and switch statements. It does not check boolean expressions. Statement coverage alone does not imply branch coverage, as shown by an example program that checks a flag but only executes one branch. The document then provides an example Java program to find prime numbers and the branch coverage results for inputs of a valid prime number, invalid prime number, and invalid data.

Uploaded by

Mallika
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

2.

BRANCH COVERAGE

BRANCH COVERAGE
 Check, whether all the branches are covered or not in the program

 It does not check the boolean expressions

 It includes

 If statements (true and false branches of each If statements)

 Nested if statements

 Switch Statements

 Statement coverage does not imply branch coverage

 Example

boolean flag=true;

If (flag==true)

[Link](“Welcome”)

 By executing only with flag==true, we will achieve statement coverage, but not branch
coverage

I. EXAMPLE OF BRANCH COVERAGE

1. SOURCE CODE
([Link])
public class JPrimeNumber {
public void findPrimeNumber(int n)
{
int c=0;
if(n!=0)
{
for(int i=0;i<n;i++)
{
if(n%(i+1)==0)
{
c++;
}
}
if(c==2)

1
{
[Link]("Given Number is Prime Number...");
}
else
{
[Link]("Given Number is Not a Prime Number...");
[Link]("Please enter valid Prime Number...");
}
}
else
{
[Link]("Plz enter a valid number");
}
}
}

TEST CLASS

([Link])

import static [Link].*;

import [Link];

public class JPrimeNumberTest {

JPrimeNumber obj=new JPrimeNumber();

// CALLING TEST CASE VIA JUNIT

@Test

public void testFindPrimeNumber() {

[Link](10);

2
2. OUTPUT
2.1 CASE 1: SUBMISSION OF VALID PRIME NUMBER
2.1.1 GENERAL AND TESTING RESULT (SUCCESS STATUS)

3
3. BRANCH & STATEMENT COVERAGE RESULT FOR INPUT ‘1’

4
4. BRANCH COVERAGE RESULT REPORT ([Link])
(INPUT: VALID PRIME NUMBER)

5
5. CASE 2: SUBMISSION OF INVALID PRIME NUMBER
5.1 GENERAL & TESTING RESULT (SUCCESS STATUS)

6
6. BRANCH COVERAGE RESULT FOR INPUT ‘2’

7
7. BRANCH COVERAGE RESULT REPORT ([Link])
(INPUT: INVALID PRIME NUMBER)

8
8. CASE 3: SUBMISSION OF INVALID INPUT DATA
8.1 GENERAL & TESTING RESULT (SUCCESS STATUS)

9
9. BRANCH COVERAGE RESULT FOR INPUT ‘3’

10
10. BRANCH COVERAGE RESULT REPORT ([Link])
(INPUT: INVALID INPUT DATA)

11

You might also like