欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

软件测试实验1

程序员文章站 2022-04-10 14:44:15
...

实验要求

  1. Install Junit(4.12), Hamcrest(1.3) with Eclipse
  2. Install Eclemma with Eclipse
  3. Write a java program for the triangle problem and test the program with Junit.
    1. 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.

实际操作

  1. 首先需要*下载junit(4.12)和Hamcrest-all(1.3)
  2. 然后打开eclipse,新建一个java的project,右键properties-Java Build Path-Library-Add External JARs
  3. 安装eclemma (这是一个开源的软件测试工具,可以查看代码调用情况、也可以检测单覆盖率)
    1. 选择Help->Eclipse Marketplace->搜索eclemma,Install
    2. 重启eclipse发现工具栏上出现Coverage图标,说明安装成功
  4. 可以开始编码写程序了。

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

测试结果:

绿色为访问到了,黄色为部分访问,红色为未访问到。
软件测试实验1

软件测试实验1