自动化测试

测试所有测试类

 import junit.framework.TestCase;
import junit.framework.Assert; /**
*测试类必须要继承TestCase类
*在junit3.8中,测试方法需要满足以下原则:
*1、public
*2。void
*3.无方法参数
*4.方法名称必须以test开头
*执行每个测试用例前会调用setup方法,后会调用tearDown方法
*/
public class CalculatorTest extends TestCase {
private Calculator cal;
public CalculatorTest(String name){
super(name);
}
@Override
public void setUp() throws Exception {
cal=new Calculator();
}
public void testAdd(){
int result=cal.add(2, 3);
Assert.assertEquals(5, result);
}
public void testSubtract(){
int result=cal.subtract(2, 3);
Assert.assertEquals(-1, result);
}
public void testMutiply(){
int result=cal.multiply(2, 3);
Assert.assertEquals(6, result);
}
public void testDivide(){
int result=0;
try {
result = cal.divide(6, 2); } catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Assert.fail();
}
Assert.assertEquals(3, result);
}
/**
* Throwable为错误和异常的父类
*/
public void testDivideByZero(){
Throwable tx=null;
try {
cal.divide(2, 0);
Assert.fail();
} catch (Exception e) {
tx=e;
e.printStackTrace();
}
Assert.assertEquals(Exception.class,tx.getClass());
Assert.assertEquals("除数不能为0", tx.getMessage());
}
}

添加进自动化测试

 import junit.extensions.RepeatedTest;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.framework.Test; /**
* 自动化测试
*
*/
public class TestAll extends TestCase {
public static Test suite(){
TestSuite suite=new TestSuite();
suite.addTestSuite(CalculatorTest.class);
suite.addTestSuite(LargestTest.class);
suite.addTestSuite(DeleteAllTest.class);
//重复执行CalculatorTest里的testAdd方法的次数
suite.addTest(new RepeatedTest(new CalculatorTest("testAdd"), 20));
return suite;
}
}

最新文章

  1. 洛谷P1726 上白泽慧音
  2. golang--gopher北京大会(1)
  3. 导出EXCEL【Web方式HTML通过拼接html中table】
  4. GPL协议的MySQL数据库
  5. html移动开发app-framework2.0使用心得
  6. Q3: Linked List Cycle II
  7. python3 split( ) not enough values to unpack(expceted 2, got 1)
  8. 最新 Arduino 驱动 12接口/户外 LED显示屏/LED点阵屏/LED单元板
  9. Js中把JSON字符串转换为JSON对象(eval()、new Function())
  10. Swift逃逸闭包之见解
  11. LDA 线性判别分析
  12. Java 常用排序算法实现--快速排序、插入排序、选择、冒泡
  13. 解决java.lang.NoSuchMethodError:org.joda.time.DateTime.withTimeAtStartOfDay() Lorg/joda/time/DateTime
  14. Groovy 设计模式 -- 适配器模式
  15. Tomcat中server.xml配置详解(2)
  16. C#模拟POST表单提交 --- WebClient
  17. socket 模拟 HTTP请求
  18. 微信小程序之可滚动视图容器组件 scroll-view
  19. CSS写表格
  20. Warm up---hdu4612(缩点,树的直径)

热门文章

  1. 使用 github.io 免费建站
  2. Python内置数据类型之Dictionary篇
  3. python练习程序(c100经典例8)
  4. Python 删除 数组
  5. 【英语】Bingo口语笔记(38) - See系列
  6. tcpdump tutorial
  7. A. Puzzles CodeForces Round #196 (Div.2)
  8. Thrift——初学
  9. 【UVa-442】矩阵链乘——简单栈练习
  10. 在python中如何设置当前工作目录