版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、第九章 junit单元测试框架,上节回顾:,junit已经13岁了,是由erich gamma 和 kent beck 为 java 语言创建了一个简单但有效的单元测试框架,。 junit 是 java 中开发单元测试的框架标准,世界上无数软件项目使用它。 支持测试代码的独立,是xp编程思想的体现。 解释:tdd,本节课内容简介,1.junit实例 2.junit核心简介 3.junit实例应用 4.junit最佳实践,9.1 junit 实例-源程序,public class computer private int a; private int b; public computer (in
2、t x, int y) a=x; b=y ; public int add() return a+b; ,public int minus() return a-b; public int multiply() return a*b; public int divide() if(b!=0) return a/b; else return 0; ,9.1 junit实例-测试代码,import junit.framework.testcase; public class testcomputer extends testcase protected void setup() throws ex
3、ception super.setup(); protected void teardown() throws exception super.teardown(); public void testadd() assertequals(25, new computer(20,5).add(); ,public void testminus() assertequals(15, new computer(20,5).minus(); public void testmultiply() assertequals(90, new computer(20,5).multiply(); public
4、 void testdivide() assertequals(4, new computer(20,5).divide(); ,9.2 junit 核心简介,操作步骤: 将 b 通过命令行方式或图形界面选择方式传递给 r,r 自动运行测试,并显示结果。,junit 框架,测试人员对testcase类进行继承,开发自己的类测试驱动程序.其余的类用来支援testcase类,比如: testsuite用来聚合多个测试用例(testcase) assert类实现期望值(expected)和实际值(actual)的验证 testresult收集所有测试用例执行后结果. test接口是这个包的关键所在,
5、它建立了testcase和testsuite之间的关联,同时为整个框架做了扩展预留.,9.2 junit 核心简介,junit 核心,testsuite,testcase,testrunner,testresult,themegallery is a design digital content public int minus(int a, int b) return a - b; public int multiply(int a, int b) return a * b; public int divide(int a, int b) throws exception if(0 = b)
6、 throw new exception(除数不能为零!); return a / b; ,该类的测试类1/4,public class calculatortest extends testcase private calculator cal; public void setup() cal = new calculator(); public void teardown() ,该类的测试类2/4,该类的测试类3/4,该类的测试类4/4,例2: maxmintool.java,public class maxmintool public static int getmax(int arr)
7、 int max = integer.min_value; for(int i = 0; i max) max = arri; return max; public static int getmin(int arr) int min = integer.max_value; for(int i = 0; i arr.length; i+) if(arri min) min = arri; return min; ,编写maxmintool.java 的测试类,public class maxmintest extends testcase public void testmax() int
8、arr = -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5; assertequals(5, maxmintool.getmax(arr); public void testmin() int arr = -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5; assertequals(-5, maxmintool.getmin(arr); ,编写测试类,显然,所准备的矩阵重复出现在两个单元测试之中,重复的程序码在设计中可以减少就尽量减少,在这两个单元测试中,整数矩阵是单元方法所需要的资源,我们称之为fixture,也就是一个测试时所需要的资源集合
9、。 对于重复出现在各个单元测试中的fixture,可以集中加以管理,可以在继承testcase之后,重新定义setup()与teardown()方法,将数个单元测试所需要的fixture在setup()中创建,并在teardown()中销毁,例如:,测试类的改进,public class maxmintest extends testcase private int arr; protected void setup() throws exception super.setup(); arr = new int-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5; protected void teardown() throws exception super.teardown(); arr = null; public void testmax() assertequals(5, maxmintool.getmax(arr); public void testmin() assertequals(-5, maxmintool.getmin(arr); setup()方法会在每
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 4s店行政考勤制度
- 各种考勤制度
- 河北保定市阜平县2025-2026学年七年级第一学期期末学业水平检测道德与法治试题(无答案)
- 安徽合肥市巢湖市2025-2026学年度第一学期期末教学质量监测八年级道德与法治试题A(无答案)
- 就寝考勤制度
- 山西居委会考勤制度
- 工作室考勤制度模板
- 工商局考勤制度
- 工程施工人员考勤制度
- 市场部门员工考勤制度
- 北京市五年(2021-2025)高考地理真题分类汇编:专题06 自然环境的整体性与差异性(解析版)
- GB/T 46161.2-2025道路车辆气压制动系第2部分:管、锥面密封外螺纹接头和螺纹孔
- GB/T 17456.1-2025球墨铸铁管、管件和附件外表面锌基涂层第1部分:带终饰层的金属锌及锌合金涂层
- 容积与容积单位课件
- 70岁以上驾驶员换证三力测试题库(附答案)
- 标准离职证明模板及注意事项
- 2026届山东省菏泽市曹县中考物理最后一模试卷含解析
- 济宁殡葬管理办法
- 银行零星装饰维修 投标方案(技术标)
- DBJ04-T495-2025 《发震断裂区域建筑抗震设计标准》
- 中考英语 三大从句(宾语从句、状语从句和定语从句)核心知识点与练习题
评论
0/150
提交评论