28 单元测试
来源:互联网 发布:乐视电视切换网络电视 编辑:程序博客网 时间:2024/06/05 20:35
Unit Test Lesson 1
1. 使用Junit的最佳实践:
1) 新建一个名为test的source folder,用于存放测试类源代码
2) 目标类与测试类应该位于同一个包下面,这样测试类中就不必导入源代码所在的包,因为他们位于同一个包下面
3) 测试类的命名规则:假如目标类是Calculator,那么测试类应该命名为TestCalculator或者是CalculatorTest
2. Junit的口号:keep the bar green to keep the code clean。
3. 我的名言:No reflection, no most frameworks
4. Junit:单元测试不是为了证明您是对的,而是为了证明您没有错误。
5. 测试用例(Test Case)是单元测试的一个很重要的方面。
6. 单元测试主要是用来判断程序的执行结果与自己期望的结果是否一致。
7. 测试类必须要继承于TestCase父类。
8. 在junit 3.8中,测试方法需要满足如下原则:
1). public的
2). void的
3). 无方法参数
10. 我们不能依赖于测试方法的执行顺序。
11. DRY(Don’t Repeat Yourself)。
12. 关于setUp与tearDown方法的执行顺序:
1) setUp
2) testAdd
3) tearDown
除数为零时,应当抛出异常。当程序执行到fail方法时,说明程序没有抛出异常
1. 使用Junit的最佳实践:
1) 新建一个名为test的source folder,用于存放测试类源代码
2) 目标类与测试类应该位于同一个包下面,这样测试类中就不必导入源代码所在的包,因为他们位于同一个包下面
3) 测试类的命名规则:假如目标类是Calculator,那么测试类应该命名为TestCalculator或者是CalculatorTest
2. Junit的口号:keep the bar green to keep the code clean。
3. 我的名言:No reflection, no most frameworks
4. Junit:单元测试不是为了证明您是对的,而是为了证明您没有错误。
5. 测试用例(Test Case)是单元测试的一个很重要的方面。
6. 单元测试主要是用来判断程序的执行结果与自己期望的结果是否一致。
7. 测试类必须要继承于TestCase父类。
8. 在junit 3.8中,测试方法需要满足如下原则:
1). public的
2). void的
3). 无方法参数
4). 方法名称必须以test开头
public void testAdd(){Caculate caculate=new Caculate();int result=caculate.add(1,2);int result2=caculate.divide(1,0);//参数第一个是期望值,第二个是实际值assertEquals(0,result2);}9. Test Case之间一定要保持完全的独立性,不允许出现任何的依赖关系。
10. 我们不能依赖于测试方法的执行顺序。
11. DRY(Don’t Repeat Yourself)。
12. 关于setUp与tearDown方法的执行顺序:
1) setUp
2) testAdd
3) tearDown
测试之前是什么状态,测试执行完毕后就应该是什么状态,而不应该由于测试执行的原因导致状态发生了变化。
aessert.fail
public void testDivide(){int result = 0;try{result = cal.divide(6, 2);}catch (Exception e){e.printStackTrace();Assert.fail();}Assert.assertEquals(3, result);}
除数为零时,应当抛出异常。当程序执行到fail方法时,说明程序没有抛出异常
测试失败
public void testDivideDivideByZero(){Throwable tx = null;try{cal.divide(6, 0);Assert.fail("测试失败");}catch(Exception ex){tx = ex;}Assert.assertEquals(Exception.class, tx.getClass());Assert.assertEquals("除数不能为0", tx.getMessage());}
测试示例
public int getMax(int []array)throws Exception{if (null ==array||0==array.length){throw new Exception("数组有误");}else {int max=0;for (int i :array){if (i>max){max =i;}}return max ;}}
public class ArrayMaxTest extends TestCase{ArrayMax arrayMax;@Overrideprotected void setUp() throws Exception{arrayMax=new ArrayMax();}public void testArrayMax(){int []a =null;Exception fx=null;try{arrayMax.getMax(a );fail();}catch (Exception e){fx =e;// TODO Auto-generated catch blocke.printStackTrace();}assertEquals(Exception.class,fx.getClass());assertEquals("数组有误",fx.getMessage());}}
测试private 方法,利用反射实现
public class Caculator2{private int add(int a,int b){return a+b;}}
public void testPrivateAdd(){Caculator2 caculator2=new Caculator2();Class<Caculator2> cal=Caculator2.class;try{ Method method=cal.getDeclaredMethod("add",new Class[]{Integer.TYPE,Integer.TYPE}); method.setAccessible(true); Object result=method.invoke(caculator2,new Object[]{1,2}); assertEquals(3+"",result+"");}catch (Exception e){// TODO Auto-generated catch blocke.printStackTrace();fail();}}
TestCase 测试套件
public static Test suite(){TestSuite suite = new TestSuite();suite.addTestSuite(CalculatorTest.class);suite.addTestSuite(LargestTest.class);suite.addTestSuite(DeleteAllTest.class);suite.addTest(new RepeatedTest(new CalculatorTest("testSubtract"), 20));return suite;}
0 0
- 28 单元测试
- 单元测试
- 单元测试
- 单元测试
- 单元测试
- 单元测试
- 单元测试
- 单元测试
- 单元测试
- 单元测试
- 单元测试
- 单元测试
- 单元测试
- 单元测试
- 单元测试:
- 单元测试
- 单元测试
- 单元测试
- oracle public redo thread and private redo thread
- XMPP协议原理介绍
- 单例的两种创建方式
- 开发成功的oracle数据库
- 排序算法——选择排序(Selection Sort)
- 28 单元测试
- 利用OpenCV的Sobel算子求得图像的梯度信息
- JS删除数组指定对象或者指定下标 删除数组重复值
- vim配置
- Memcached Client Forjava API
- MyISAM 和 InnoDB 讲解
- uc伯克利人工分割图像.seg文件解析
- log4net使用详解
- [Python]网络爬虫(一):抓取网页的含义和URL基本构成