Software Testing Experiment 2 White Box Testing

1. Purpose of the experiment

1. Master the 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 program segments of the following judgment levels , which are required to satisfy 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, input 90, 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 Jun it to unit test the following program. (Any one group is designed 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;
       }
}

3. Experimental steps

1. Use logical coverage

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

A program's control flow graph is a graphical way of describing the control flow of a program. A control flow graph is a degenerate program flow diagram in which each process in the graph degenerates into a node, and streamlines become directed arcs connecting different nodes.

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

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

① In a selective or multi-branch structure, there should be a convergent node at the convergent point of the branch;

② The area delineated by edges and nodes is called an area. When counting areas, the area outside the graph should also be recorded as an area.

2. Basic path method

1) The basic path test is based on the program control flow graph, by analyzing the complexity of the loop of the control structure, and deriving the basic executable path set, so as to design the test case method.

2) The basic path test mainly includes the following four aspects:

  (1) Draw the program control flow graph of the program. According to the program flow chart, draw the control flow graph of the program. (Denote the node with the number or line number before the code line)

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

There are three ways to calculate the complexity of a cycle:

① Define the cycle complexity as V(G), E is the number of edges in the control flow graph, and V is the number of nodes in the control flow graph, then there is a 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 the independent path. Derive the set of basic paths through the program's control flow graph, listing the independent paths of the program.

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

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

1 ) Class Person is tested using JUnit:

(1) First create a new class Person:

(2) Create a test class:

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

So, what do we need to pay attention to during the whole test step?

➢The  test method must use @Test

➢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 methods cannot have any dependencies

2. Use Eclipse to create JUnit test classes

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 for the test class directory, as shown in the figure:

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

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

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

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

Then, modify your test code on this basis.

Explanation of Failure and error in the test results:

 Failure is generally caused by the test unit using the assertion method to judge the failure. This error indicates that the test point has found a problem, that is, the result of the program output is different from what we expected

 The error is caused by a code exception, which can generate an error in the code itself, or a hidden bug in the test code

What would the generated code look like if we checked these four methods?

After checking, generate the code as shown in the figure:

Similarly, right click, select [Run As], click [JUnit Test], the running result:

From this, we conclude as follows:

 @BeforeClass

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

 @AfterClass

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

 @Before 和@After

Will be executed once before and after each test method.

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

4. Experimental results

1. Logic coverage method

Control flow chart:

Test case:

( 1 ) Test cases for statement coverage:

serial number

test case

statement

execution path

1

x=100,y=100

1,3

1,2,3,8,9

2

x=85,y=85

1,5

1,2,4,5,7,8,9

3

x=60,y=60

1,7

1,2,4,6,7,8,9

( 2 ) Test cases for determining coverage:

serial number

test case

judgement

execution path

1

x=100,y=100

2,4

1,2,3,8,9

2

x=85,y=85

2,4

1,2,4,6,7,8,9

3

x=60,y=60

2,4

1,2,4,6,7,8,9

( 3 ) Test cases covered by conditions:

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 ) Test cases for decision - condition coverage

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

( 5 ) Test cases covered by condition combinations

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 loop 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=2

2=2

1-2-3-4-9

2

n=3

3=3

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

3

n=4

4=2*2

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

3. Unit testing

code:

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;
       }
}

5. Experimental summary

Various problems were encountered during the experiment, and the method used in the test was not clearly understood. Through discussions with the students, all the problems were solved and the experiment was successfully completed.

Guess you like

Origin blog.csdn.net/qq_64314976/article/details/131367238