android 单元测试方法
来源:互联网 发布:龙源数据库论文查询 编辑:程序博客网 时间:2024/06/05 06:39
对于android开发,有时候会遇到单独对某个接口进行测试,如果每一次都要写一个activity工程去测试,就显得太麻烦,如果不需要activity,单独对某个接口进行测试。
具体方法如下:
1.建立一个测试类,该类继承AndroidTestCase(new--android Junit test case,输入类名称,包名,选择brower继承的父类为AndroidTestCase)
package com.example.test;import android.test.AndroidTestCase;public class uinitest extends AndroidTestCase {public void testunit() throws Throwable{ service.PrintfLog("Hi,This unit test 1"); service obj=new service(); obj.printfLog2("Hi,This is unit test 2");};}
2,创建一个服务类(new--Class, 输入包名,类名,如有继承,选择继承的父类)。
package com.example.test;import android.util.Log;public class service { private final static String tag="Mytag";static void PrintfLog(String log){Log.i(tag,log);}public void printfLog2(String log){Log.i(tag,log);}}
3.修改清单文件,注册test类
<instrumentation android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.example.test" android:label="Test" />
<uses-library android:name="android.test.runner" />
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.test" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <uses-library android:name="android.test.runner" /> <activity android:name="com.example.test.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> </application><instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.example.test" android:label="Test" /></manifest>
4.执行单元测试
选择接口方法---右键菜单----debug as----android Junit test
测试结果统计,如果出错,进度条为红色,且有错误统计,否则为绿色
logCat打印为:
0 0
- Android:Junit 单元测试方法
- android 单元测试方法
- Android 单元测试方法
- Android 单元测试方法与步骤
- Android 单元测试方法与步骤
- android 单元测试方法 类继承 Instrumentation
- Android添加单元测试的方法与步骤
- android 单元测试简单方法及错误处理
- 四.Android单元测试 PowerMock mock静态方法
- 单元测试方法
- 单元测试方法
- 单元测试方法
- 单元测试方法
- 单元测试方法
- 单元测试方法
- android 单元测试
- Android单元测试
- Android单元测试
- 恒天云技术分享系列5 – 虚拟化平台性能对比(KVM & VMware)
- 第二学期第12周项目1--长颈鹿类对动物类的继承
- java io 流
- C# - LINQ
- unix环境编程 UDP通信
- android 单元测试方法
- 理解WPF中的视觉树和逻辑树
- JZ2440v2开发版下载程序到Nand Flash
- 如何创建service
- ubuntu12.04编译内核流程
- 第二学期第12周项目2--教师兼干部类
- MyEclipse 提示设置JSP自动提示
- ASP.NET Web API
- 软考小结