测试用例&测试套件

举个栗子:

  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. }

运行结果如图:

keep the bar green to keep the code clean——Junit详解(二)的更多相关文章

  1. keep the bar green to keep the code clean——Junit详解(一)

    测试用例 单元测试时每个开发人员必需掌握的,是保证开发过程中代码的准确性,无误性,保证代码质量.敏捷开发模式是先根据用户需求写测试用例,考虑基本所有用户所需要的情况,再写实现方法.单元测试有很多种,当 ...

  2. junit单元测试(keeps the bar green to keeps the code clean)

    error是程序错误,failure是测试错误. junit概要: JUnit是由 Erich Gamma (设计模式的创始人)和 Kent Beck (敏捷开发的创始人之一)编写的一个回归测试框架( ...

  3. 单元测试JUnit 4(二)——keeps the bar green to keeps the code clean

    1.Failure和Error Failure是指测试失败  Error是指测试程序本身出错  (int a=10/0) 2.JUnit常用注解 2.1 @RunWith: 可以更改测试运行器(继承o ...

  4. 单元测试JUnit 4 (一)——keeps the bar green to keeps the code clean

    1. 导读 Junit是一个可编写重复测试的简单框架,是基于Xunit架构的单元测试框架的实例.Junit4最大的改进是大量使用注解(元数据),很多实际执行过程都在Junit的后台做完了,而且写tes ...

  5. [转]OAuth 2.0 - Authorization Code授权方式详解

    本文转自:http://www.cnblogs.com/highend/archive/2012/07/06/oautn2_authorization_code.html I:OAuth 2.0 开发 ...

  6. 【转】Code First 属性详解

    下面解释每个配置的作用 Table :用于指定生成表的表名.架构信息. Column :用于指定生成数据表的列信息,如列名.数据类型.顺序等. Key :用于指定任何名称的属性作为主键列并且默认将此列 ...

  7. OAuth 2.0 - Authorization Code授权方式详解

    I:OAuth 2.0 开发前期准备 天上不会自然掉馅饼让你轻松地去访问到人家资源服务器里面的用户数据资源,所以你需要做的前期开发准备工作就是把AppKey, AppSecret取到手 新浪获取传送门 ...

  8. iOS Code Signing: 解惑详解

    iPhone开发的代码签名 代码签名确保代码的真实以及明确识别代码的来源.在代码运行在一个开发系统以前,以及在代码提交到Apple发布以前,Apple要求所有的的应用程序都必须进行数字签名.另外,Ap ...

  9. Action Bar详解(二)

    在Android3.0之后,Google对UI导航设计上进行了一系列的改革,其中有一个非常好用的新功能就是引入的ActionBar,他用于取代3.0之前的标题栏,并提供更为丰富的导航效果. 一.添加A ...

随机推荐

  1. Jboss配置之数据源密码配置密文--EncryptingDataSourcePasswords

    local-tx-datasource:最常用的数据源配置,该连接池的连接管理器是LocalTxConnectionManager,只支持本地事务,不适合做分布式事务.以mssql为例如下:如果密码按 ...

  2. django queryset values&values_list

    values返回是字典列表; values_list返回的是元组列表, values_list加上 flat=True 1 1 之后返回值列表

  3. FtpClient.storeFile返回false解决方法

    在确定路径和文件名没有中文的情况下添加以下代码 ftp.setFileTransferMode(ftp.BINARY_FILE_TYPE); ftp.enterLocalPassiveMode();/ ...

  4. 通过nginx代理之后,获取客户端ip

    1.相关nginx配置(通过header将客户端ip,host等信息传入) location ~ .*.do$ { proxy_set_header X-Real-IP $remote_addr; p ...

  5. js基础:函数表达式和函数声明

    函数表达式和函数声明的区别.实际上,解析器在向执行环境中加载数据是,对函数表达式和函数声明并非一视同仁.解析器会率先读取函数声明,并使其在执行任何代码之前可用.而函数表达式,则必须等到解析器执行到它所 ...

  6. iOS10 拍照崩溃问题

    根据相对应得功能添加相关权限即可,没必要全部添加,后面的描述可以官方点,因为会以弹出框的形式访问的,比如相机权限后面的描述可以为:这个应用需要访问相机:后续如果发现其他iOS10上面的问题会及时更新的

  7. Java里面获取当前服务器的IP地址

    public static void main(String[] args) { try { InetAddress address = InetAddress.getLocalHost();//获取 ...

  8. tcpdump用法

    http://man.linuxde.net/tcpdump http://www.cnblogs.com/yc_sunniwell/archive/2010/07/05/1771563.html

  9. 【积累】发送验证码按钮倒计时js

    注册的时候要发送验证码,就上网研究了一下,写了一个简单点的... jsp页面: <input type="button" id="testbtn" val ...

  10. 转!!windows记事本保存“联通” 编码问题

    原博文网址:http://blog.csdn.net/Zhiyuan_Ma/article/details/51838054 简单分析: 这是微软记事本的一个BUG,准确点就是unicode编码的问题 ...