• 软件测试(三)——上机使用JUnit进行测试


     使用Junit和 eclemma进行对程序的测试。

    程序内容:输入3个整型变量,判断他们是否能构成等边三角形、等腰三角形或一般三角形。

    一、安装Junit,Hamcrest和eclemma

    直接通过右键项目——Properties——Java Build Path——Libraries——Add External JARs

    将junit 和 hamcrest的包导入即可。

    eclemma:

    help→install new software→add

    注意红色的这里不要选,否则会很慢

    然后选择EclEmma直接下载安装。安装后重启即可。

    二、编写Triangle类

    在src文件夹下创建com.Triangle包,然后创建Triangle.java类

     1 package com.Triangle;
     2 
     3 public class Triangle {
     4     public String positiveTest(int a, int b, int c){
     5         //判断边长是否为正整数
     6         if(a>0 && b>0 && c>0) return "Passed";
     7         else{
     8             String report = "";
     9             if(a<=0) report+="1st ";
    10             if(b<=0) report+="2nd ";
    11             if(c<=0) report+="3rd ";
    12             report += "Not Positive";
    13             return report;
    14         }
    15     }
    16     public String canFormTriangle(int a, int b, int c){
    17         //判断三条边是否能形成三角形
    18         if(a+b>c && b+c>a && c+a>b) return "Passed";
    19         else return "Not Triangle";
    20     }
    21     public String isoscelesTest(int a, int b, int c){
    22         //判断是否为等腰三角形
    23         if(a==b || b==c || c==a) return "Passed";
    24         else return "Not Isosceles";
    25     }
    26     public String equilateralTest(int a, int b, int c){
    27         //判断是否为等边三角形
    28         if(a==b && b==c) return "Passed";
    29         else return "Not Equilateral";
    30     }
    31     public String test(int a, int b, int c){
    32         //完整的测试
    33         String report="";
    34         report = positiveTest(a,b,c);
    35         if(!report.equals("Passed")) return report;
    36         else{
    37             report = canFormTriangle(a,b,c);
    38             if(!report.equals("Passed")) return report;
    39             else{
    40                 report = isoscelesTest(a,b,c);
    41                 if(!report.equals("Passed")) return "Scalene";
    42                 else{
    43                     report = equilateralTest(a,b,c);
    44                     if(!report.equals("Passed")) return "Isosceles";
    45                     else{
    46                         return "Equilateral";
    47                     }
    48                 }
    49             }
    50         }
    51     }
    52 }
    Triangle.java

    二、编写测试类

    新建source folder JUnitTest,里面同样创建com.Triangle包。

    由于Triangle里面涉及到不止一个方法。因此为每一个方法都创建测试类。

    canFormTriangleTest.java

    测试类里,用到了@Parameters 进行参数化测试。由于每个使用参数化进行测试的类都只有一套输入参数,因此不能将对多个方法的测试放到同一个类里。

     1 package com.Triangle;
     2 
     3 import static org.junit.Assert.*;
     4 
     5 import java.util.Arrays;
     6 import java.util.Collection;
     7 import org.junit.Before;
     8 import org.junit.Test;
     9 import org.junit.runner.RunWith;
    10 import org.junit.runners.Parameterized;
    11 import org.junit.runners.Parameterized.Parameters;
    12 
    13 import com.Triangle.Triangle;
    14 
    15 @RunWith(Parameterized.class)
    16 public class IsEquilateralTest {
    17     //对equilateralTest()进行测试
    18     private String expected = null;
    19     private int a;
    20     private int b;
    21     private int c;
    22     private Triangle triangle = null;
    23 
    24     public IsEquilateralTest(int a, int b, int c, String expected) {
    25         this.a = a;
    26         this.b = b;
    27         this.c = c;
    28         this.expected = expected;
    29     }
    30 
    31     @Before
    32     public void setUp() {
    33         triangle = new Triangle();
    34     }
    35 
    36     @Parameters
    37     public static Collection<Object[]> getData() {
    38         return Arrays.asList(new Object[][] { 
    39                 { 3,3,3,"Passed" }, 
    40                 { 3,5,3,"Not Equilateral" },
    41                 { 4,1,3,"Not Equilateral" } 
    42                 });
    43     }
    44 
    45     @Test
    46     public void testAdd() {
    47         assertEquals(this.expected, triangle.equilateralTest(a, b, c));
    48     }
    49 }
    IsEquilateralTest
     1 package com.Triangle;
     2 
     3 import static org.junit.Assert.*;
     4 
     5 import java.util.Arrays;
     6 import java.util.Collection;
     7 
     8 import org.junit.Before;
     9 import org.junit.Test;
    10 import org.junit.runner.RunWith;
    11 import org.junit.runners.Parameterized;
    12 import org.junit.runners.Parameterized.Parameters;
    13 
    14 import com.Triangle.Triangle;
    15 
    16 @RunWith(Parameterized.class)
    17 public class IsIsoscelesTest {
    18     private String expected = null;
    19     private int a;
    20     private int b;
    21     private int c;
    22     private Triangle triangle = null;
    23 
    24     public IsIsoscelesTest(int a, int b, int c, String expected) {
    25         this.a = a;
    26         this.b = b;
    27         this.c = c;
    28         this.expected = expected;
    29     }
    30 
    31     @Before
    32     public void setUp() {
    33         triangle = new Triangle();
    34     }
    35 
    36     @Parameters
    37     public static Collection<Object[]> getData() {
    38         return Arrays.asList(new Object[][] { 
    39                 { 3,3,3,"Passed" }, 
    40                 { 3,5,3,"Passed" },
    41                 { 4,1,3,"Not Isosceles" } 
    42                 });
    43     }
    44 
    45     @Test
    46     public void testAdd() {
    47         assertEquals(this.expected, triangle.isoscelesTest(a, b, c));
    48     }
    49 }
    IsIsoscelesTest
     1 package com.Triangle;
     2 
     3 import static org.junit.Assert.*;
     4 
     5 import java.util.Arrays;
     6 import java.util.Collection;
     7 
     8 import org.junit.Before;
     9 import org.junit.Test;
    10 import org.junit.runner.RunWith;
    11 import org.junit.runners.Parameterized;
    12 import org.junit.runners.Parameterized.Parameters;
    13 
    14 import com.Triangle.Triangle;
    15 
    16 @RunWith(Parameterized.class)
    17 public class PositiveTest {
    18     private String expected = null;
    19     private int a;
    20     private int b;
    21     private int c;
    22     private Triangle triangle = null;
    23 
    24     public PositiveTest(int a, int b, int c, String expected) {
    25         this.a = a;
    26         this.b = b;
    27         this.c = c;
    28         this.expected = expected;
    29     }
    30 
    31     @Before
    32     public void setUp() {
    33         triangle = new Triangle();
    34     }
    35 
    36     @Parameters
    37     public static Collection<Object[]> getData() {
    38         return Arrays.asList(new Object[][] { 
    39                 { 3,3,3,"Passed" }, 
    40                 { 3,5,3,"Passed" },
    41                 { 4,-1,3,"2nd Not Positive" } 
    42                 });
    43     }
    44 
    45     @Test
    46     public void testAdd() {
    47         assertEquals(this.expected, triangle.positiveTest(a, b,c));
    48     }
    49 }
    PositiveTest
     1 package com.Triangle;
     2 
     3 import static org.junit.Assert.*;
     4 import java.util.Arrays;
     5 import java.util.Collection;
     6 import org.junit.Before;
     7 import org.junit.Test;
     8 import org.junit.runner.RunWith;
     9 import org.junit.runners.Parameterized;
    10 import org.junit.runners.Parameterized.Parameters;
    11 
    12 import com.Triangle.Triangle;
    13 
    14 @RunWith(Parameterized.class)
    15 public class TriangleTestCase {
    16     private String expected = null;
    17     private int a;
    18     private int b;
    19     private int c;
    20     private Triangle triangle = null;
    21 
    22     public TriangleTestCase(int a, int b, int c, String expected) {
    23         this.a = a;
    24         this.b = b;
    25         this.c = c;
    26         this.expected = expected;
    27     }
    28 
    29     @Before
    30     public void setUp() {
    31         triangle = new Triangle();
    32     }
    33 
    34     @Parameters
    35     public static Collection<Object[]> getData() {
    36         return Arrays.asList(new Object[][] {
    37                 {-1,1,1,"1st Not Positive"},{0,1,1,"1st Not Positive"},
    38                 {-1,1,1,"1st Not Positive"},{1,0,1,"2nd Not Positive"},
    39                 {1,1,-1,"3rd Not Positive"},{1,1,0,"3rd Not Positive"},
    40                 {-1,-1,1,"1st 2nd Not Positive"},{-1,1,-1,"1st 3rd Not Positive"},{1,-1,-1,"2nd 3rd Not Positive"},
    41                 {-1,-1,-1,"1st 2nd 3rd Not Positive"},
    42                 {1,1,2,"Not Triangle"},{1,2,1,"Not Triangle"},{2,1,1,"Not Triangle"},
    43                 {1,1,3,"Not Triangle"},{1,3,1,"Not Triangle"},{3,1,1,"Not Triangle"},
    44                 {3,4,5,"Scalene"},{6,8,10,"Scalene"},
    45                 {2,2,3,"Isosceles"},{3,2,2,"Isosceles"},{2,3,2,"Isosceles"},
    46                 {3,3,3,"Equilateral"}
    47                 });
    48     }
    49 
    50     @Test
    51     public void testAdd() {
    52         assertEquals(this.expected, triangle.test(a, b, c));
    53     }
    54 }
    TriangleTestCase

    最后可以创建一个JUnit Test suite类,将上面五个测试类放到一起,统一进行测试。

     1 package com.Triangle;
     2 
     3 import org.junit.runner.RunWith;
     4 import org.junit.runners.Suite;
     5 import org.junit.runners.Suite.SuiteClasses;
     6 
     7 @RunWith(Suite.class)
     8 @SuiteClasses({ CanFormTriangleTest.class, IsEquilateralTest.class,
     9         IsIsoscelesTest.class, PositiveTest.class, TriangleTestCase.class })
    10 public class AllTests {
    11 
    12 }
    AllTests.java

    三、使用EclEmma进行测试

    可以对每个测试类分别进行测试。也就是对每一个方法单独进行测试。

    例如,右键canFormTriangleTest.java,选择Coverage As——JUnit Test进行测试

    结果:

    由于只是对Triangle里的一部分进行测试,所以覆盖率很小

    如果是对TriangleTestCase进行测试,那么会发现对Triangle.java覆盖率就达到100%了

    最后,通过AllTests测试时,就可以达到对所有测试类的100%覆盖了(除了AllTests之外)

  • 相关阅读:
    C++模板类报错:函数未定义
    linux top命令和sar命令
    XAMPP的服务在CentOS中无法启动
    XAMPP安装LAMP后无法使用数据库
    关于try catch语句中finally代码块有没有必要的思考
    eclipse卡死的两种解决办法
    VS2019 配置 OpenCV-4.4.0
    VS2019+OpenCV4配置报错:模块计算机类型x64与目标计算机类型x86冲突
    slide js用法 公告 广告 新闻滚动
    input type:text输入框点击输入,文字消失
  • 原文地址:https://www.cnblogs.com/sywang/p/5292812.html
Copyright © 2020-2023  润新知