目录

  • 单元测试

  • UI 测试

原文链接: Unit and UI Testing in Android Studio

1 单元测试

  • 配置

  • 编码

  • 测试

1.1 配置

1.1.1 IDE 配置
Build Variants => Test Artifact => Unit Tests

1.1.2 build.gradle

dependencies {compile fileTree(dir: 'libs', include: ['*.jar'])compile 'com.android.support:appcompat-v7:22.1.1'testCompile 'junit:junit:4.12'}

1.1.3 Sync project
Tools => Android => Sync Project With Gradle Files

1.2 编码

1.2.1 被测类 Calculator

public class Calculator {    public double sum(double a, double b){        return 0;    }    public double substract(double a, double b){        return 0;    }    public double divide(double a, double b){        return 0;    }    public double multiply(double a, double b){        return 0;    }}

1.2.2 测试类 CalculatorTest

  • app/src 目录建立目录 test/java

  • app/src/test/java 目录下自动生成类 CalculatorTest

// CalculatorTest.javaimport org.junit.Before;import org.junit.Test;import static org.junit.Assert.*;public class CalculatorTest {    private Calculator mCalculator;    @Before    public void setUp() throws Exception {        mCalculator = new Calculator();    }    @Test    public void testSum() throws Exception {        //expected: 6, sum of 1 and 5        assertEquals(6d, mCalculator.sum(1d, 5d), 0);    }    @Test    public void testSubstract() throws Exception {        assertEquals(1d, mCalculator.substract(5d, 4d), 0);    }    @Test    public void testDivide() throws Exception {        assertEquals(4d, mCalculator.divide(20d, 5d), 0);    }    @Test    public void testMultiply() throws Exception {        assertEquals(10d, mCalculator.multiply(2d, 5d), 0);    }}

1.3 测试

右键点击 CalculatorTest 类,选择 Run > CalculatorTest 。也可以通过命令行运行测试,在工程目录内输入:

./gradlew test

1.4 测试结果

由于没有对 Calculator 进行具体实现,测试全部失败。实现后重新测试即可通过。

更多相关文章

  1. Android计算文件夹大小、文件大小单位转换、删除文件夹及其内容
  2. Android(安卓)cocos2d 弹弓游戏 Catapult 源代码
  3. Android(安卓)单元测试 Error: ShouldNotReachHere()
  4. “Nothing to push” error in Android(安卓)studio with Git
  5. Android(安卓)开发Tips 之 Bmob操作
  6. Study:基于Selenium进行Android客户端自动化测试的例子。
  7. zz:Android(安卓)APP Monkey信息自动收集脚本
  8. android Imageview 资源动态更改颜色
  9. android 读取doc文档

随机推荐

  1. Android中的时间自动更新
  2. Android对Linux内核的改动你知道多少
  3. 在你的android设备运行java web应用程序
  4. Android内存管理
  5. 丢失Android系统库或者Conversion to Dal
  6. android简介
  7. 电子书 android高薪之路-android程序员面
  8. android的消息处理机制(图+源码分析)——Lo
  9. 【Android面试知识点系列】Handler相关
  10. 开发Android主攻四大方向