The experiment of software testing
Experiment Three white Box test
Experimental purpose
(1) Learn white box test method
(2) Mastering the logic covering methods of statement coverage, conditional coverage, branch coverage, etc.
(3) Mastering the use of Java Code Analysis tools
Experimental content
1. computes the greatest common divisor of an integer x and an integer y. (not allowed in the way the classroom is used)
L Use classes and methods (define a class, define a greatest common divisor method in the Class), and name it according to the specification.
L GET the two integers entered by the user in the main method, call the previously written method, and output their greatest common divisor.
L use FindBugs to find out if there are any bugs in the program.
2. Application of Logic overlay
L write out the test cases covered by the statement, the branch coverage, and the path it covers, according to the program flowchart given.
L Add-on: According to the program flowchart, write out the code (define a class and method to implement), generate unit tests with JUnit, and test using the test cases previously designed.
1.
Package Cn.zhi.mju;
import Java.util.Scanner;
/**
*
* @author Chenyazhi
*
*/
Public class Testthree {
/**
*
* @param args
*/
Public Static void Main (string[] args) {
Testthree testthree = new testthree ();
Testthree.view ();
}
Public void view () {
Scanner in = new Scanner (System. in);
int number1 = 1;
int number2 = 1;
System. out. Print ("Please enter first number:");
Try {
Number1=in.nextint ();
if (number1==0) {
number1=10/0;
}
} catch (Exception e) {
TODO: Handle exception
System. out. println ("You are not entering a number or a number of 0, please re-enter!") ");
Testthree testthree = new testthree ();
Testthree.view ();
}
System. out. Print ("Please enter a second number:");
Try {
Number2=in.nextint ();
if (number2==0) {
number2=10/0;
}
} catch (Exception e) {
TODO: Handle exception
System. out. println ("You are not entering a number or a number of 0! , please re-enter! ");
Testthree testthree = new testthree ();
Testthree.view ();
}
Output Greatest common divisor
System. The greatest common divisor of out. println (number1+ "and" +number2+ ") are:" +GCM(number1,number2) ");
}
/**
*
* @param number1 First Number
* @param number2 Second number
* Returns a smaller value
*/
Public Static int min (int number1,int number2) {
if (Number1>number2)
return number2;
Else
return number1;
}
Public Static int GCM (int number1,int number2) {
Find out greatest common divisor
int e = 1;
for (int i=1;i<=min(number1,number2); i++) {
if (min(number1,number2)%i==0&& (number1+number2)%i==0) {
E=i;
}
}
return e;
}
}
2.
Coverage path: AEG, AEF, ABC, ABD (1)
Statement overrides:
Coverage path: AEG, AEF, ABC
Test Case: X=5,y=0
X=4,y=0
x=y=2
Branch Coverage:
Test Case: X=5,y=0
X=4,y=0
x=y=2
X=3,y=0
(2)
Package Cn.zhi.mju;
import Java.util.Scanner;
/**
*
* @author Chenyazhi
*
*/
Public class White_box {
Public Static void Main (string[] args) {
/**
*
* @param main function
*/
Scanner in = new Scanner (System. in);
System. out. println ("Please input x value:");
int x = In.nextint ();
System. out. println ("Please input y value:");
int y = in.nextint ();
if (X<4 | | y>0) {
if (y>1) {
y=y+1;
}
}Else{
if (x>=5) {
X=x-y;
}
Else {
X=x+y;
}
}
System. out. println ("x Value:" +x+ ", y value:" +y+ ".");
}
}
The third experiment