[Unit Testing] Junit 4 Tutorial (1)--White Box Testing Method

Table of contents

1.0 Flowchart Identification

1.1 Statement coverage method (C0 standard)

1.2 Judgment/branch coverage method (C1 standard)

1.3 Conditional coverage method (C2 standard)

1.4 Judgment condition coverage method (C1+C2 standard)

1.5 Condition combination coverage method (C3 standard)

1.6 Path coverage

1.7 Basic Path Covering Method

 epilogue


1.0 Flowchart Identification

1.1 Statement coverage method (C0 standard)

① target

Every executable statement in the program is executed at least once

If it is not explained later, this example is still used

Program source code:

import java.util.Scanner;

public class Demo {
	public static void main(String[] args) {
		int a, b;
		double c;
		Scanner scanner = new Scanner(System.in);
		a = scanner.nextInt();
		b = scanner.nextInt();
		c = scanner.nextDouble();
		if (a > 0 && b > 0) {
			c = c / a;
		} else if (a > 1 || c > 1) {
			c = c + 1;
		}
		c = b + c;
		System.out.println("a=" + a);
		System.out.println("b=" + b);
		System.out.println("c=" + c);
		scanner.close();
	}
} 
Draw a flowchart:

② use case

enter output
a = 2 b = 1 c = 6 a = 2 b = 1 c = 7

③ Advantages and disadvantages

  • Statement coverage can find statement errors
  • Statement coverage cannot find logic errors

1.2 Judgment/branch coverage method (C1 standard)

① target

​ The true branch and false branch of each decision in the program are executed at least once

② use case

enter output
a = 2 b = 1 c = 6 a = 2 b = 1 c = 5
a = -1 b = 1 c = 1 a = -1 b = 1 c = 7

③ Advantages and disadvantages

  • Decision/branch coverage can find logic errors
  • Decision/branch coverage cannot find conditional errors in combined judgments

1.3 Conditional coverage method (C2 standard)

① target

​ The possible value of each condition in each judgment in the program is satisfied at least once

② use case

Judgment 3: (a > 0 && b > 0)

Judgment 6: (a > 1 || c > 1)

​ Atomic conditions:

​ a > 0

​ b > 0

​ a > 1

​ c > 1

Requirement: Each atomic condition set is true and false once

enter atomic conditions Judgment conditions
a = -1 b = 2 c = 3 a>0 is false, b>0 is true, a>1 is false, c>1 is true Judgment 3 is false, judgment 6 is true
a = 2 b = -1 c = -2 a>0 is true, b>0 is false, a>1 is true, c>1 is false Judgment 3 is false, judgment 6 is true

③ Advantages and disadvantages

  • Not necessarily more comprehensive than decision/branch coverage
  • Cannot detect logic errors

1.4 Judgment condition coverage method (C1+C2 standard)

① target

​All possible values ​​in each  condition are executed at least once , and at the same time, the possible results of each decision are executed at least once

② use case

enter atomic conditions Judgment conditions
a = 2 b = 1 c = 4 a>0 is true, b>0 is true, a>1 is true, c>1 is true Judgment 3 is true, judgment 6 is true
a = -1 b = -2 c = -3 a>0 is false, b>0 is false, a>1 is false, c>1 is false Judgment 3 is false, judgment 6 is false

③ Advantages and disadvantages

  • may cause some conditions to mask others

1.5 Condition combination coverage method (C3 standard)

① target

​ All conditional value combinations in each judgment are executed at least once

② use case

Judgment 3: (a > 0 && b > 0)

Judgment 6: (a > 1 || c > 1)

​ Atomic conditions:

​ a > 0 b > 0 a > 1 c > 1

Require:

​ When a>0, b>0 is true and false once

​ When a<=0, b>0 is true and false once

​ When a>1, c>1 is true and false once

​ When a<=1, c>1 is true and false once

enter atomic conditions Judgment conditions
a = 2 b = 1 c = 6 a>0 is true, b>0 is true, a>1 is true, c>1 is true Judgment 3 is true, judgment 6 is true
a = 2 b = -1 c = -2 a>0 is true, b>0 is false, a>1 is true, c>1 is false Judgment 3 is false, judgment 6 is true
a = -1 b = 2 c = 3 a>0 is false, b>0 is true, a>1 is false, c>1 is true Judgment 3 is false, judgment 6 is true
a = -1 b = -2 c = -3 a>0 is false, b>0 is false, a>1 is false, c>1 is false Judgment 3 is false, judgment 6 is false

③ Advantages and disadvantages

  • More comprehensive than condition coverage

1.6 Path coverage

① target

​ Use cases cover all possible execution paths in a program

② use case

enter atomic conditions Judgment conditions
a = 2 b = 1 c = 6 a>0 is true, b>0 is true, a>1 is true, c>1 is true Judgment 3 is true, judgment 6 is true
a = 1 b = 1 c = -3 a>0 is true, b>0 is true, a>1 is false, c>1 is false Judgment 3 is true, judgment 6 is false
a = -1 b = 2 c = 3 a>0 is false, b>0 is true, a>1 is false, c>1 is true Judgment 3 is false, judgment 6 is true
a = -1 b = -2 c = -3 a>0 is false, b>0 is false, a>1 is false, c>1 is false Judgment 3 is false, judgment 6 is false

③ Advantages and disadvantages

  • unrealistic
    • because of the relatively long and almost infinite number of paths involved
    • Any possible loop is considered as a possible path in the block

1.7 Basic Path Covering Method

1) McCabe's base path method

2) Number of linearly independent paths from source node to sink node (calculated according to cyclomatic complexity)

  1. V(G) = e - n + 2p = 10 - 7 + 2 = 5
  2. When the scale is small, we can visually identify independent paths

3) Given below is a path represented by a sequence of nodes/edges:

  1. p1:A,B,C,G/1,4,9
  2. p2:A,B,C,B,C,G/1,4,3,4,9
  3. p3:A,B,E,F,G/1,5,8,10
  4. p4:A,D,E,F,G/2,6,8,10
  5. p5:A,D,F,G/2,7,10

The above is the whole content of this section, if there is any mistake, please correct me!


 


 epilogue

This post ends here, and finally, I hope that friends who read this post can gain something.

 Obtaining method: leave a message [software testing and learning]

If you think the article is not bad, please like, share, and leave a message , because this will be the strongest motivation for me to continue to output more high-quality articles!

 

Guess you like

Origin blog.csdn.net/weixin_67553250/article/details/131310003