版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 创新思维与护理质量提升
- 2026年高中必修二材料测试题及答案
- 2026年股市知识测试题及答案
- 2026年小学英语知识测试题及答案
- 2026年稀有圈基础测试题及答案
- 护理就业市场信息与职业发展
- 中风急性期中医护理原则
- 2026年外向孤独症心理测试题及答案
- 2026年family tree测试题及答案
- 2026年军事体检心理测试题及答案
- 2026云南昆明供电局项目制用工招聘48人笔试模拟试题及答案解析
- 2026云南高创人才服务有限公司招聘6人笔试备考试题及答案解析
- 全胃切除病人全程营养管理中国专家共识(2026版)
- 2026年四川成都市中考地理试卷含答案
- 2025-2026 学年人音版初中音乐八年级下册全册知识点梳理
- 2026年自贡市自流井区社区工作者招聘笔试参考试题及答案解析
- 2026年版闲鱼卖货实战手册(选品+定价+爆款打造完整攻略)
- 雨课堂学堂在线学堂云审计法律研究与案例(西南政法大学)单元测试考核答案
- “十五五”规划纲要应知应会100题及答案
- 2026安徽合肥市发展和改革委员会上半年招聘事业单位工作人员20人考试备考试题及答案解析
- 限额以下小型工程常见安全隐患指导手册(2026版)
评论
0/150
提交评论