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