软件测试实验1
程序员文章站
2022-04-10 14:44:15
...
实验要求
- Install Junit(4.12), Hamcrest(1.3) with Eclipse
- Install Eclemma with Eclipse
- Write a java program for the triangle problem and test the program with Junit.
- Description of triangle problem:
Function triangle takes three integers a,b,c which are length of triangle sides; calculates whether the triangle is equilateral, isosceles, or scalene.
- Description of triangle problem:
实际操作
- 首先需要*下载junit(4.12)和Hamcrest-all(1.3)
- 然后打开eclipse,新建一个java的project,右键properties-Java Build Path-Library-Add External JARs
- 安装eclemma (这是一个开源的软件测试工具,可以查看代码调用情况、也可以检测单覆盖率)
- 选择Help->Eclipse Marketplace->搜索eclemma,Install
- 重启eclipse发现工具栏上出现Coverage图标,说明安装成功
- 可以开始编码写程序了。
lab1.Main
package lab1;
public class Main {
//whether this three integers can make a triangle
public Boolean triangle(int a,int b,int c) {
if(a <= 0 || b <= 0 || c <= 0 ||
a+b <= c || a+c <= b || b+c <= a) return false;
return true;
}
public Boolean equilateral(int a,int b,int c) {
if(triangle(a,b,c) && a == b && b == c) return true;
return false;
}
public Boolean isosceles(int a,int b,int c) {
if(triangle(a,b,c) &&
(a == b || b == c || a == c) ) return true;
return false;
}
/*
* 0 not a triangle
* 1 equilateral
* 2 isosceles
* 3 triangle
*/
public int tri(int a,int b,int c) {
if(!triangle(a,b,c)) return 0;
if(equilateral(a,b,c)) return 1;
if(isosceles(a,b,c)) return 2;
return 3;
}
public static void main(String[] args) {
}
}
右键new一个junit Test case,进行测试。
MainTest
package lab1;
import static org.junit.Assert.*;
import org.junit.Test;
public class MainTest {
/*
* 0 not a triangle
* 1 equilateral
* 2 isosceles
* 3 triangle
*/
@Test
public void test() {
Main l = new Main();
assertEquals(0,l.tri(-2, 3, 32));
assertEquals(0,l.tri(2, 3, 32));
assertEquals(0,l.tri(0, 0, 1));
assertEquals(0,l.tri(0, 0, 0));
assertEquals(0,l.tri(-2, -2, -2));
assertEquals(1,l.tri(3, 3, 3));
assertEquals(2,l.tri(8, 6, 6));
assertEquals(2,l.tri(2, 3, 2));
assertEquals(2,l.tri(2, 1, 2));
assertEquals(3,l.tri(2, 3, 4));
assertEquals(3,l.tri(2, 5, 6));
assertEquals(3,l.tri(3, 4, 5));
}
}
测试结果:
绿色为访问到了,黄色为部分访问,红色为未访问到。