上篇我们已经讲到怎么在android工程中使用JUnit测试,http://blog.csdn.net/chaoyue0071/article/details/45223115

接下来我们测试案例的周期,异常,当有多个测试文件时怎么用测试套件TestSuit:

我们所写的每一个test开头的方法就是一个测试用例,每一个测试用例都是单独存在的。每一个测试用例运行之前都会调用setUp方法,当一个测试用例执行之后会调用tearDown方法。

package com.wangjialin.junit.lifecycle;import junit.framework.Assert;import junit.runner.*;import android.test.AndroidTestCase;import android.util.Log;import com.wangjialin.junit.service.Calculator;/** *  * * junit 3.8 *  * 1单元测试方法需要声明为public类型 * 2单元测试方法的返回值类型为void * 3按照JUnit3的规范要求单元测试方法的方法命名需要以test开头 * 4单元测试方法的参数必须为空 * 5单元测试方法最好声明向单元测试框架抛出异常 */public class CalculatorTest extends AndroidTestCase {private Calculator calculator;private static final String TAG = "CalculatorTest";@Overrideprotected void setUp() throws Exception {super.setUp();calculator = new Calculator();Log.i(TAG, "单元测试用例生命周期的初始化方法被调用");}@Overrideprotected void tearDown() throws Exception {super.tearDown();Log.i(TAG, "单元测试用例生命周期中的结束方法被调用");}public void testAdd() throws Throwable{//calculator = new Calculator();Log.i(TAG, "testAdd has been invoked!!!");int result = calculator.add(1, 2);Assert.assertEquals(3, result);}public void testSubtract() throws Throwable{//calculator = new Calculator();Log.i(TAG, "testSubtract has been invoked!!!");int result = calculator.subtract(1, 2);Assert.assertEquals(-1, result);}public void testMultiply() throws Throwable{//calculator = new Calculator();Log.i(TAG, "testMultiply has been invoked!!!");int result = calculator.multiply(2, 3);Assert.assertEquals(6, result);}public void testDivide() throws Throwable{//calculator = new Calculator();Log.i(TAG, "testDivide has been invoked!!!");int result = calculator.divide(6, 2);Assert.assertEquals(3, result);}public void testDivideDivideByZero(){//calculator = new Calculator();Log.i(TAG, "testDivideDivideByZero has been invoked!!!");Throwable exception = null;try{calculator.divide(6, 0);Assert.fail("测试失败");}catch(Exception ex){exception = ex;}Assert.assertEquals(Exception.class, exception.getClass());Assert.assertEquals("除数不能为0", exception.getMessage());}}

通过log输出可以看出每个用例都会调用setUp方法和tearDown方法,每个方法也都抛出异常

如果方法不抛出异常,可以加try catch语句。

接下来是自动化测试TestSuit

package com.wangjialin.junit.testsuite;import junit.framework.Test;import junit.framework.TestCase;import junit.framework.TestSuite;/** *  * * 测试类必须要继承于TestCase父类 */public class TestAll extends TestCase {/** *  * 1自动化测试方法必须声明为public * 2自动化测试方法必须声明为static类型的 * 3自动化测试方法必须返回Test接口类型的对象 * 4自动化测试方法的名称必须为suite * @return */public static Test suite(){TestSuite suite = new TestSuite();suite.addTestSuite(CalculatorTest.class);suite.addTestSuite(CalculatorAnotherTest.class);return suite;}}

CalculatorTest.class和CalculatorAnotherTest.class都是需要测试文件。再选中TestAll文件右键运行即可。

更多相关文章

  1. React Native 适配Android物理返回键,实现连续两次点击退出
  2. android如何调用其他应用的方法或属性
  3. Android(安卓)单元测试和日志输出
  4. 编程回忆之Android回忆(Android标题栏的去除和全屏)
  5. Eclipse的项目迁移到Android(安卓)Studio的方法。
  6. 使用Kotlin高效地开发Android(安卓)App(一)
  7. HandlerThread
  8. Android中的消息机制(Handler)
  9. android之数据库的使用案例

随机推荐

  1. Android与H5互调
  2. android 中遥控器键值的添加和修改
  3. Android中运用Pull解析器读取XML文件
  4. Android RIL源码分析(2)
  5. Android 3D引擎之CatCake----编译hello_c
  6. Andriod 开发之微信分享接口
  7. Android中的Intent详解
  8. 【Android】Android背景选择器selector用
  9. Java/Android引用类型及其使用分析
  10. Android Activity切换动画overridePendin