White box testing (structural testing)

1.Purpose of the experiment

1. Master the equal logic coverage testing method;

2. Master the basic path testing method;

3. Master Junit based unit testing.

2.Experimental content

1. Design a set of test cases for the following judgment level program segments, which are required to meet statement coverage, judgment coverage, condition coverage, judgment-condition coverage, and condition combination coverage respectively.

public char function(int x, int y) {
1. char t;
2. if((x>= 90) &&(y>= 90)){
3.    t ='A';
}else {
4.   if((x+ y)>= 165){
5.     t = 'B';
} else{
6.    t = 'C';
7.   }
8. }
9. return t;
}

2. The function of the following program code (Java language) is to decompose a positive integer into prime factors. For example, enter 90 and print out 90=2*3*3*5. It is required to use the basic path method to design test cases.

public static void zhiyinshu( int n){
1. int k=2;
1. System.out.print(n + "="); //输出:n= 
2. while(k<= n){
3.   if(k == n){
4.     System.out.println(n); // 输出: n 
4.     break; 
}else {
5.     if(n%k==0){
6.        System.out.print(k +"*");//输出:k*
6.        n=n/k; /
}else {
7.        k++;
8.      }
9. }
}

3. Use Junit to unit test the following program. (Design any 1 group for each method)

public class Calculator {
	public double add(double a,double b){
	    return a+b;
	}
	public double sub(double a,double b){
	    return a-b;
	}
	public double multiply(double a, double b){
	    return a*b;
	}
	public double div(double a, double b){
	    return a/b;
	}
}

Three, Experimental steps

1. Use logical coverage method

1) Draw the control flow graph of the program (use the number or line number before the code line to represent the node), and mark each condition with Ti and Fi.

The control flow diagram of a program is a graphical method of describing the control flow of a program. A control flow graph is a degenerated program flow graph. Each process in the graph is degenerated into a node, and the streamlines become directed arcs connecting different nodes.

Among them, the circle is called a node of the control flow graph and represents one or more branchless statements or source program statements; the arrow is called an edge or connection and represents the control flow.

When simplifying the program flow diagram into a control flow diagram, you should pay attention to:

① In a selection or multi-branch structure, there should be a convergence node at the convergence of branches;

②The area enclosed by edges and nodes is called a region. When counting regions, the area outside the graph should also be recorded as a region.

2. Basic path method

1) Basic path testing is a method of designing test cases based on the program control flow graph by analyzing the loop complexity of the control structure and deriving a basic executable path set.

2) Basic path testing mainly includes the following four aspects:

  (1) Draw the program control flow graph of the program. Draw the control flow diagram of the program based on the program flow chart. (Use the number or line number before the line of code to indicate the node)

  (2) Calculate the program cycle complexity. Cyclomatic complexity is a software metric that provides a quantitative measure of a program's logical complexity. This measure is used to calculate the number of basic independent paths of a program, which is the minimum number of test cases that must be executed at least once for each executable statement in the program. number.

There are three ways to calculate cyclomatic complexity:

① Define the loop complexity as V(G), E is the number of edges of the control flow graph, V is the number of nodes of the control flow graph, then there is the formula V(G) = E-N+2;

② Define P as the number of decision nodes in the control flow graph, then there is the formula V (G) = P + 1;

③The number of regions in the control flow graph is R, then V(G)=R.

  (3) Find independent paths. Export a basic path set through the program's control flow graph, listing the independent paths of the program.

  (4) Design test cases. Design use case input data and expected results based on program structure and program loop complexity to ensure the execution of each path in the basic path set.

3. Use Junit to unit test the following program. (P106)

1)Class PersonUse JUnit for testing:

(1)First create a new class Person:

(2)Create test class:

(3) Right-click, select [Run As], and click [JUnit Test]

So, what do we need to pay attention to during the entire testing step?

 @Test must be used in the test method

 The test method must be modified with public void

 Create a new directory to store the test code

 The package of the test class should be the same as the package of the tested class, as shown in the figure:

 Each method in the test unit must be able to be tested independently, and its method cannot have any dependencies

2. Use Eclipse to create a JUnit test class

Case 2: Use tools to create JUnit test classes and test them.

Proceed as follows:

1) Select the method to be tested, right-click New, and select other

2) Select [JUnit Test Case] ​​under [JUnit] under [java], as shown in the figure:

3) Select test in the test category directory, as shown in the figure:

4) Click [Next], as shown in the figure:

5) Check the method you want to test and click [Finish] as shown in the figure:

6) In this step, if the JUnit jar package has not been added before, a prompt will pop up:

Just click [OK]. If the jar package has been added, this prompt will not pop up. Finally, the generated code is as follows

Then, modify your test code based on this.

Explanation of Failure and error in the test results:

 Failure is generally caused by the failure of the test unit to use the assertion method. This error indicates that the test point has discovered a problem, that is, the program output result is different from what we expected< /span>

 error is caused by a code exception. It can produce an error in the code itself, or it can be a hidden bug in the test code

What will the code generated look like if we check these four methods?

After checking, the code is generated as shown in the figure:

Similarly, right-click, select [Run As], click [JUnit Test], and the running results are:

From this, we summarize as follows:

 @BeforeClass

The modified method will be executed before all methods are called, and the method is static, so it will be run after the test class is loaded, and there will only be one instance of it in the memory. It is more suitable for loading configuration files. (Executed only once for all tests).

 @AfterClass

The modified method is usually used for resource management, such as closing a database connection (only executed once for all tests).

 @Before 和@After

It will be executed once before and after each test method.

 @Test: Test method, here you can test the expected exception and timeout period

fourth, implementationresult

1. Logical coverage method

Control flow chart:

Test case:

(1)Statement coverage test case:

serial number

test case

statement

Execution path

1

x=90,y=90

1,3,9

1-2-3-8-9

2

x=89,y=89

1,5,9

1-2-4-5-7-8-9

3

x=80,y=80

1,6,9

1-2-4-6-7-8-9

(2) Determine the test cases that covers:

serial number

test case

judgement

Execution path

1

x=85,y=85

2,4

1-2-4-6-7-8-9

2

x=60,y=60

2,4

1-2-4-6-7-8-9

3

x=100,y=100

2,4

1-2-3-8-9

(3) Condition covers the test case of

serial number

test case

condition

Execution path

1

x=100,y=100

2,4

1-2-3-8-9

2

x=85,y=85

2,4

1-2-4-5-7-8-9

3

x=60,y=60

2,4

1-2-4-6-7-8-9

(4) Judgment-Condition cover a>Examination example

serial number

test case

condition

Execution path

1

x=90,y=90

2,4

1-2-3-8-9

2

x=90,y=89

2,4

1-2-4-5-7-8-9

3

x=89,y=90

2,4

1-2-4-5-7-8-9

4

x=80,y=80

2,4

1-2-4-6-7-8-9

(5) Condition combination coverage test case

serial number

test case

condition

Execution path

1

x=100,y=100

2,4

1-2-3-8-9

2

x=90,y=85

2,4

1-2-4-6-7-8-9

3

x=85,y=90

2,4

1-2-4-5-7-8-9

4

x=60,y=60

2,4

1-2-4-6-7-8-9

2. Basic path method

Program control flow graph:

Program cycle complexity: V(G)=P+1=3+1=4

Independent path:

1-2-9

1-2-3-4-9

1-2-3-5-7-8-2-3-4-9

1-2-3-5-6-8-2-3-4-9

Test case:

serial number

Input data

expected output

Execution path

1

n=1

1=

1-2-9

2

n=2

2=2

1-2-3-4-9

3

n=3

3=3

1-2-3-5-7-8-2-3-4-9

4

n=4

4=2*2

1-2-3-5-6-8-2-3-4-9

3. Unit testing

Five, Experimental Summary

If you don’t have a clear understanding of statement coverage, condition coverage, etc., you can easily solve it by asking your classmates.

Guess you like

Origin blog.csdn.net/qq_70311894/article/details/133420821