zoukankan      html  css  js  c++  java
  • keep the bar green to keep the code clean——Junit详解(二)

    测试用例&测试套件

    举个栗子:

    1. 编写MyStack类模拟栈,并对其进行测试用例编写测试;

       

    2. 编写文件删除方法,并对其删除测试。 不再做演示,戳此获取代码

    MyStack类:

    1. public class MyStatck {
    2.  
    3.    private String[] elements;
    4.    private int nextIndex;
    5.  
    6.    public MyStatck() {
    7.       elements = new String[100];
    8.       nextIndex = 0;
    9.    }
    10.  
    11.    public void push(String element) throws Exception {
    12.       if (nextIndex >= 100) {
    13.          throw new Exception("数组越界异常!");
    14.       }
    15.       elements[nextIndex++] = element;
    16.    }
    17.  
    18.    public String pop() throws Exception {
    19.       if (nextIndex <= 0) {
    20.          throw new Exception("数组越界异常!");
    21.       }
    22.       return elements[--nextIndex];
    23.    }
    24.  
    25.    public String top() throws Exception {
    26.       if (nextIndex <= 0) {
    27.          throw new Exception("数组越界异常!");
    28.       }
    29.       return elements[nextIndex - 1];
    30.    }
    31.  
    32. }

    对push方法编写测试:

    1. public void testPush(){
    2.       MyStatck myStatck = new MyStatck();
    3.       //测试用例中对方法抛出的异常进行try-catch处理。
    4.       try {
    5.          myStatck.push("Hello World!");
    6.       } catch (Exception e) {
    7.          Assert.fail("push方法异常,测试失败。");
    8.       }
    9.       String result = null;
    10.       try {
    11.          result = myStatck.pop();
    12.       } catch (Exception e) {
    13.          e.printStackTrace();
    14.       }
    15.       //验证断言压入的字符串是否为"Hello World!"。
    16.       Assert.assertEquals("Hello World!", result);
    17.    }

    虽然testPush测试用例中调用了pop方法,但对pop方法仍要创建新的测试用例:

    测试中是可以使用其他方法,不然就没法进行测试了

    1. public void testPop(){
    2.  
    3.    MyStatck myStatck = new MyStatck();
    4.    try {
    5.       myStatck.push("Hello World!");
    6.    } catch (Exception e) {
    7.       e.printStackTrace();
    8.    }
    9.    String result = null;
    10.    try {
    11.       result = myStatck.pop();
    12.    } catch (Exception e) {
    13.       Assert.fail("pop方法异常,测试失败。");
    14.    }
    15.    //验证断言弹出的字符串是否为"Hello World!"。
    16.    Assert.assertEquals("Hello World!", result);
    17.  
    18. }

    两个测试方法大致相同,但是侧重点不同。侧重对测试方法的断言判断。

    每个test case只做一件事情,只测试一个方面。

    随着项目的开发,类越来越多,测试也越来越多。单个测试的机械动作也会拖慢速度。那么就需要更简便的方法,只要点一下,就可以测试全部的测试用例——这种方法就是使用测试套件。

    测试套件(TestSuite):可以将多个测试组合到一起,同时执行多个测试

    创建测试套件约定:

    1. 在test源目录内创建测试类;
    2. 创建public static Test suite(){}方法。

       

    贴代码

    1. public class TestAll extends TestCase {
    2.  
    3.    public static Test suite(){
    4.  
    5.       TestSuite suite = new TestSuite();
    6.       suite.addTestSuite(CalcTest.class);
    7.       suite.addTestSuite(DeleteAllTest.class);
    8.       suite.addTestSuite(MyStatckTest.class);
    9.  
    10.       return suite;
    11.  
    12.    }
    13. }

    运行结果如图:

  • 相关阅读:
    GSON -JSON 反序列化-多节点名称支持
    Jedis 分片原理
    闭锁-CountDownLatch
    XML序列化及反序列化
    用GIT操作SVN
    报表worker-CPU使用率过高原因排查
    二.PlantUML 之活动图
    一.PlantUML 与 IDEA 集成
    ArrayList
    VI常用命令
  • 原文地址:https://www.cnblogs.com/dannybear/p/4823151.html
Copyright © 2011-2022 走看看