keep the bar green to keep the code clean——Junit详解(二)
测试用例&测试套件
举个栗子:
|
MyStack类:
- public class MyStatck {
- private String[] elements;
- private int nextIndex;
- public MyStatck() {
- elements = new String[100];
- nextIndex = 0;
- }
- public void push(String element) throws Exception {
- if (nextIndex >= 100) {
- throw new Exception("数组越界异常!");
- }
- elements[nextIndex++] = element;
- }
- public String pop() throws Exception {
- if (nextIndex <= 0) {
- throw new Exception("数组越界异常!");
- }
- return elements[--nextIndex];
- }
- public String top() throws Exception {
- if (nextIndex <= 0) {
- throw new Exception("数组越界异常!");
- }
- return elements[nextIndex - 1];
- }
- }
对push方法编写测试:
- public void testPush(){
- MyStatck myStatck = new MyStatck();
- //测试用例中对方法抛出的异常进行try-catch处理。
- try {
- myStatck.push("Hello World!");
- } catch (Exception e) {
- Assert.fail("push方法异常,测试失败。");
- }
- String result = null;
- try {
- result = myStatck.pop();
- } catch (Exception e) {
- e.printStackTrace();
- }
- //验证断言压入的字符串是否为"Hello World!"。
- Assert.assertEquals("Hello World!", result);
- }
虽然testPush测试用例中调用了pop方法,但对pop方法仍要创建新的测试用例:
测试中是可以使用其他方法,不然就没法进行测试了
- public void testPop(){
- MyStatck myStatck = new MyStatck();
- try {
- myStatck.push("Hello World!");
- } catch (Exception e) {
- e.printStackTrace();
- }
- String result = null;
- try {
- result = myStatck.pop();
- } catch (Exception e) {
- Assert.fail("pop方法异常,测试失败。");
- }
- //验证断言弹出的字符串是否为"Hello World!"。
- Assert.assertEquals("Hello World!", result);
- }
两个测试方法大致相同,但是侧重点不同。侧重对测试方法的断言判断。
每个test case只做一件事情,只测试一个方面。
随着项目的开发,类越来越多,测试也越来越多。单个测试的机械动作也会拖慢速度。那么就需要更简便的方法,只要点一下,就可以测试全部的测试用例——这种方法就是使用测试套件。
测试套件(TestSuite):可以将多个测试组合到一起,同时执行多个测试
创建测试套件约定:
- 在test源目录内创建测试类;
- 创建public static Test suite(){}方法。
贴代码
- public class TestAll extends TestCase {
- public static Test suite(){
- TestSuite suite = new TestSuite();
- suite.addTestSuite(CalcTest.class);
- suite.addTestSuite(DeleteAllTest.class);
- suite.addTestSuite(MyStatckTest.class);
- return suite;
- }
- }
运行结果如图:
keep the bar green to keep the code clean——Junit详解(二)的更多相关文章
- keep the bar green to keep the code clean——Junit详解(一)
测试用例 单元测试时每个开发人员必需掌握的,是保证开发过程中代码的准确性,无误性,保证代码质量.敏捷开发模式是先根据用户需求写测试用例,考虑基本所有用户所需要的情况,再写实现方法.单元测试有很多种,当 ...
- junit单元测试(keeps the bar green to keeps the code clean)
error是程序错误,failure是测试错误. junit概要: JUnit是由 Erich Gamma (设计模式的创始人)和 Kent Beck (敏捷开发的创始人之一)编写的一个回归测试框架( ...
- 单元测试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 ...
- 单元测试JUnit 4 (一)——keeps the bar green to keeps the code clean
1. 导读 Junit是一个可编写重复测试的简单框架,是基于Xunit架构的单元测试框架的实例.Junit4最大的改进是大量使用注解(元数据),很多实际执行过程都在Junit的后台做完了,而且写tes ...
- [转]OAuth 2.0 - Authorization Code授权方式详解
本文转自:http://www.cnblogs.com/highend/archive/2012/07/06/oautn2_authorization_code.html I:OAuth 2.0 开发 ...
- 【转】Code First 属性详解
下面解释每个配置的作用 Table :用于指定生成表的表名.架构信息. Column :用于指定生成数据表的列信息,如列名.数据类型.顺序等. Key :用于指定任何名称的属性作为主键列并且默认将此列 ...
- OAuth 2.0 - Authorization Code授权方式详解
I:OAuth 2.0 开发前期准备 天上不会自然掉馅饼让你轻松地去访问到人家资源服务器里面的用户数据资源,所以你需要做的前期开发准备工作就是把AppKey, AppSecret取到手 新浪获取传送门 ...
- iOS Code Signing: 解惑详解
iPhone开发的代码签名 代码签名确保代码的真实以及明确识别代码的来源.在代码运行在一个开发系统以前,以及在代码提交到Apple发布以前,Apple要求所有的的应用程序都必须进行数字签名.另外,Ap ...
- Action Bar详解(二)
在Android3.0之后,Google对UI导航设计上进行了一系列的改革,其中有一个非常好用的新功能就是引入的ActionBar,他用于取代3.0之前的标题栏,并提供更为丰富的导航效果. 一.添加A ...
随机推荐
- drawable animation
drawable 动画,帧动画: 1 定义动画xml文件 <?xml version="1.0" encoding="utf-8"?> <an ...
- 最小化安装的CentOS7挂载ntfs格式的U盘
准备从系统中拷贝一些文件到U盘,插上U盘. 一.获得U盘的设备识别符 fdisk -l 啊哈,我看到了,是/dev/sdb1 二.熟练的挂载 mount /dev/sdb1 /mnt/usb Duan ...
- LayoutControl让一个控件占据多行或者多列
拖动一个layoutcontrol到form上之后,会自动附带一个layoutgroup 设置layoutgroup的layoutmode为table 设置layoutgroup的OptionsTab ...
- 部署wcf到IIS时的问题
1,部署到IIS后,在浏览器可以访问.但客户端添加服务引用时,出现错误: - 下载“http://admin-pc/IISHostService/Service1.svc?xsd=xsd0”时出错.- ...
- google play下载apk
http://apps.evozi.com/apk-downloader/ 输入apk下载地址
- AVSampleBufferDisplayLayer----转
http://blog.csdn.net/fernandowei/article/details/52179631 目前大多数iOS端的视频渲染都使用OpenGLES,但如果仅仅为了渲染而不做其他的例 ...
- Calibre - book library management application
http://calibre-ebook.com/ Library Management E-book conversion Syncing to e-book reader devices Down ...
- 无法删除对象 '产品',因为该对象正由一个 FOREIGN KEY 约束引用。
在删除northwindcs表时,发生报错,消息 3726,级别 16,状态 1,第 2 行,无法删除对象 '产品',因为该对象正由一个 FOREIGN KEY 约束引用.此时判断是因为有其他表的外键 ...
- comparator接口与compare方法的实现
刷leetcodecode时看到一道题需要利用自定义的比较器进行排序,最开始一头雾水,看了API终于懂了~ Arrays.sort(T[] a,Comparator<? super T> ...
- ArcGIS发布服务时缓存切片设置
[文件]>[共享]>[服务]>[覆盖原有服务]或[创建新服务] 设置好相关参数后,会弹出"服务编辑框": 进入"缓存" 1."绘制此 ...