第一步:在AndroidManifest.xml中加入如下两段代码:

  1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  2. package="com.pccw"
  3. android:versionCode="1"
  4. android:versionName="1.0">
  5. <uses-sdk android:minSdkVersion="8" />
  6. <application android:icon="@drawable/icon" android:label="@string/app_name">
  7. <activity android:name=".MainActivity"
  8. android:label="@string/app_name">
  9. <intent-filter>
  10. <action android:name="android.intent.action.MAIN" />
  11. <category android:name="android.intent.category.LAUNCHER" />
  12. </intent-filter>
  13. </activity>
  14. <!—添加代码1-->
  15. <uses-library android:name="android.test.runner"/>
  16. </application>
  17. <!—添加代码2-->
  18. <instrumentation android:name="android.test.InstrumentationTestRunner"
  19. android:targetPackage="com.pccw" android:label="aaa"/>
  20. </manifest>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.pccw"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MainActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!—添加代码1-->
		<uses-library android:name="android.test.runner"/>
</application>
    <!—添加代码2-->
		<instrumentation android:name="android.test.InstrumentationTestRunner"
			android:targetPackage="com.pccw" android:label="aaa"/>
</manifest>

1.         < uses-library android:name="android.test.runner"/>代表把单元测试框架中的一些依赖库引入进来

2.         < instrumentation android:name="android.test.InstrumentationTestRunner"android:targetPackage="com.pccw" android:label="aaa"/>代表配置单元测试框架的启动装置,启动装置有好几个类,可以选择,一般情况下我们使用上面这个。

3.         targetPackage与上面的package相同,代表单元测试框架和当前应用是处于同一个进程中

第二步:编写业务逻辑,即需要被测试的模块

  1. public class PersonService {
  2. public void save(String name){
  3. String sub = name.substring(6);
  4. }
  5. public int add(int a, int b){
  6. return a+b;
  7. }
  8. }
public class PersonService {

	public void save(String name){
		String sub = name.substring(6);
	}

	public int add(int a, int b){
		return a+b;
	}
}

第三步:编写单元测试代码

  1. public class PersonServiceTest extends AndroidTestCase {
  2. public void testSave() throws Exception {
  3. PersonService service = new PersonService();
  4. service.save(null);
  5. }
  6. public void testAdd() throws Exception {
  7. PersonService service = new PersonService();
  8. int result = service.add(1, 2);
  9. Assert.assertEquals(3, result);
  10. }
  11. }
public class PersonServiceTest extends AndroidTestCase {

	public void testSave() throws Exception {
		PersonService service = new PersonService();
		service.save(null);
	}

	public void testAdd() throws Exception {
		PersonService service = new PersonService();
		int result = service.add(1, 2);
		Assert.assertEquals(3, result);
	}
}

第四步:打开eclipse中的outline窗口,其中会显示单元测试类的所有的方法

然后想要测试哪个方法,则在哪个测试方法上右键鼠标,选择Run As,然后再选择Android JUnit Test即可,如果有异常或者错误,则会出现如下情况:

如果是正常的,则会如下:

Android学习笔记:对Android应用进行单元测试的更多相关文章

  1. Android学习笔记1 android adb启动失败问题 adb server is out of date. killing...

    下面是Android的学习笔记,原文地址. 我是使用adb devices出现如下红字错误, 使用第一种方法方法,结果关掉豌豆荚就可以了. android adb启动失败问题 adb server i ...

  2. Android学习笔记之Android Studio添加新的Activity

    1.创建Android项目工程:AndroidTest 创建过程可参考网上诸多教程. 2.添加新的Activity,步骤如下 a. 在layout文件夹上右键,New-Activity-相应Activ ...

  3. Android学习笔记之 android:collapseColumns ,android:shrinkColumns 和stretchColumns

    摘自:http://blog.csdn.net/sjf0115/article/details/7213565/ TableLayout是一个使用复杂的布局,最简单的用法就仅仅是拖拉控件做出个界面,但 ...

  4. android学习笔记45——android的数据存储和IO

    android的数据存储和IO SharedPreferences与Editor简介 SharedPreferences保存的数据主要是类似于配置信息格式的数据,因此其保存的数据主要是简单的类型的ke ...

  5. Android学习笔记之Android Studio下创建menu布局文件

    1.创建menu文件夹 Android Studio项目中如果没有menu文件夹,在res文件夹右键,new-Android resource directory: 则会弹出下图对话框,在Resour ...

  6. Android学习笔记_79_ Android 使用 搜索框

    1.在资源文件夹下创建xml文件夹,并创建一个searchable.xml: android:searchSuggestAuthorityshux属性的值跟实现SearchRecentSuggesti ...

  7. android学习笔记(9)android程序调试学习

    相应若水老师的第十四课 一,Log日志输出 Log.v(tag,message);        //verbose模式,打印最具体的日志  Log.d(tag,message);        // ...

  8. Android学习笔记(1)—Android Studio安装

    Android Studio 是一个全新的 Android 开发环境,基于IntelliJ IDEA. 类似 Eclipse ADT,Android Studio 提供了集成的 Android 开发工 ...

  9. Android学习笔记1——Android开发环境配置

    一.JDK配置 Android是基于Java进行开发的,首先需要在电脑上配置JDK(Java Development Kit).在http://www.androiddevtools.cn/下载对应系 ...

  10. Android学习笔记(36):Android的两种事件处理方式

    Android提供了两种事件处理的方式:基于回调的事件处理 和 基于监听的事件处理. 我们来说的easy理解一点: (1)基于回调的事件处理就是继承GUI组件,并重写该组件的事件处理方法.除了一些特定 ...

随机推荐

  1. centos gnome面板菜单任务栏消失后的解决

    今天终于下决心把我的电脑装成了centos,上网的时候感觉上边的那一行菜单栏碍事儿,就把他给删了 就是桌面最顶上这一行东西,然后百度无果,谷歌上不去,用bing好不容易才找到里解决方案! 症状:进入l ...

  2. delphi 验证码识别(XE8源码)

    如题:源码下载

  3. 利用Python进行数据分析——Numpy基础:数组和矢量计算

    利用Python进行数据分析--Numpy基础:数组和矢量计算 ndarry,一个具有矢量运算和复杂广播能力快速节省空间的多维数组 对整组数据进行快速运算的标准数学函数,无需for-loop 用于读写 ...

  4. POJ 3050 Hopscotch DFS

    The cows play the child's game of hopscotch in a non-traditional way. Instead of a linear set of num ...

  5. Go 语言变量作用域

    作用域为已声明标识符所表示的常量.类型.变量.函数或包在源代码中的作用范围. Go 语言中变量可以在三个地方声明: 函数内定义的变量称为局部变量 函数外定义的变量称为全局变量 函数定义中的变量称为形式 ...

  6. Docker控制组

    控制组是 Linux 容器机制的另外一个关键组件,负责实现资源的审计和限制. 它提供了很多有用的特性:以及确保各个容器可以公平地分享主机的内存.CPU.磁盘 IO 等资源:当然,更重要的是,控制组确保 ...

  7. Django 跨域请求处理

    参考https://blog.csdn.net/qq_27068845/article/details/73007155 http://blog.51cto.com/aaronsa/2071108 d ...

  8. leetcode之Largest Rectangle in Histogram

    问题来源:Largest Rectangle in Histogram 问题描述:给定一个长度为n的直方图,我们可以在直方图高低不同的长方形之间画一个更大的长方形,求该长方形的最大面积.例如,给定下述 ...

  9. Python 性能剖分工具

    Python 性能剖分工具 眼看着项目即将完成,却被测试人员告知没有通过性能测试,这种情况在开发中屡见不鲜.接下来的工作就是加班加点地找出性能瓶颈,然后进行优化,再进行性能测试,如此这般周而复始直到通 ...

  10. Appium移动自动化测试(五)--app控件获取之uiautomatorviewer

    初探 在Android的SDk提供了以下的工具来支持我们进行UI自动化测试: uiautomatorviewer:用来扫描和分析Android应用程序的UI控件的工具. uiautomator:一个包 ...