第一步:在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即可,如果有异常或者错误,则会出现如下情况:

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

最新文章

  1. nyoj 218 Dinner(贪心专题)
  2. tengine-2.1.0 源码安装
  3. 攻城狮在路上(贰) Spring(二)--- Spring IoC概念介绍
  4. JAVA实例,判断是否是瑞年
  5. Android应用程序插件化研究之AssertManager
  6. oracle xe client 如何设置 tnsnames.ora(解决无法使用pl/sql developer的问题)
  7. python实现算24的算法
  8. RabbitMQ学习笔记一:本地Windows环境安装RabbitMQ Server
  9. dom定位的三种元素
  10. mysql 修改字段
  11. swift - UIWebView 和 WKWebView(iOS12 之后替换UIWebView)
  12. Markdown,你只需要掌握这几个
  13. 安卓调试桥(ADB)环境变量的配置
  14. 2016-2017-2 20155309南皓芯java第五周学习总结
  15. OC BLOCK和协议
  16. egret学习记录
  17. Appium+python自动化19-iOS模拟器(iOS Simulator)安装自家APP
  18. MariaDB存在的问题
  19. Zero Sum Subarray
  20. 常用模块(sys)

热门文章

  1. 如何在joomla上展示word,pdf,xlsx,ppt
  2. Go 语言变量
  3. 2017-暑假作业-Java语言程序设计
  4. Dynamics 365 Web Api之基于single-valued navigation property的filter查询
  5. Redis Cluster架构优化
  6. Switch控件详解
  7. Swift:消除Null值
  8. 剑指Offer——京东实习笔试题汇总
  9. Apache shiro集群实现 (八) web集群时session同步的3种方法
  10. 使用Intent传递对象