Andriod 自动化测试—InstrumentationTestCase:
1,新建一个andriod project:
2,编写MainActivity代码,如:
package cj.andriodtest.com;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class App extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView myText = (TextView) findViewById(R.id.text1);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
myText.setText("Hello Android");
}});
}
public int add(int i, int j)
{
return (i + j);
}
}
3,编写测试代码,如:
package cj.andriodtest.test;
import cj.andriodtest.com.App;
import android.content.Intent;
import android.os.SystemClock;
import android.test.InstrumentationTestCase;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;
public class SampleTest extends InstrumentationTestCase {
private App app = null;
private Button button = null;
private TextView text = null;
/*
* 初始设置,该方法第一个被调用,完成初始化工作
*/
@Override
protected void setUp() {
try {
super.setUp();
} catch (Exception e) {
e.printStackTrace();
}
Intent intent = new Intent();
//System.out.println("---------------------------------------------------");
intent.setClassName("cj.andriodtest.com", App.class.getName());
System.out.println("99999999999999999");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//获取被测对象context
app = (App) getInstrumentation().startActivitySync(intent);
//System.out.println("Apppppppppp: "+app);
//获取被测试的程序的资源要加包名,不然无法获取
text = (TextView) app.findViewById(cj.andriodtest.com.R.id.text1);
//System.out.println("textttttttt: "+text);
//获取被测试的程序的资源要加包名,不然无法获取
button = (Button)app.findViewById(cj.andriodtest.com.R.id.button1);;
}
/*
* 垃圾清理与资源回收 ,测试用例完成时调用
*
*/
@Override
protected void tearDown() {
app.finish();
try {
super.tearDown();
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* 活动功能测试
*/
public void testActivity() throws Exception {
Log.v("testActivity", "test the Activity");
SystemClock.sleep(1500);
//getInstrumentation().runOnMainSync(new PerformClick(button));
getInstrumentation().runOnMainSync(new PerformClick(button));
SystemClock.sleep(3000);
assertEquals("Hello Android", text.getText().toString());
}
/*
*模拟按钮点击的接口
*/
private class PerformClick implements Runnable {
Button btn;
public PerformClick(Button button) {
btn = button;
}
public void run() {
btn.performClick();
}
}
/*
* 测试类中的方法
*/
public void testAdd() throws Exception {
String tag = "testAdd";
Log.v(tag, "test the method");
int test = app.add(1, 1);
assertEquals(2, test);
}
}
4,配置AndriodManifest.xml文件,将测试代码与被测试程序关联:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cj.andriodtest.com"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<!--需要加载测试扩展包-->
<uses-library android:name="android.test.runner" />
<activity android:name=".App"
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>
<uses-sdk android:minSdkVersion="8" />
<!--加载被测主程序包名-->
<instrumentation android:targetPackage="cj.andriodtest.com" android:name="android.test.InstrumentationTestRunner" />
</manifest>
5,运行测试程序,运行测试程序有2种方式:
1> 在andriodproject 上右击,run as Andriod JUnit Test,直接运行测试用例
2>在模拟器上,运行测试用例。在模拟器中选择dev tools->“andriod.test.InstrumentationTestRunner”
来运行测试用例。这种很好,可以不需要pc运行测试用例。
注意:dev tools 真机上可以没有,需要安装
分享到:
相关推荐
深入理解Android自动化测试 PDF 下载深入理解Android自动化测试 PDF 下载深入理解Android自动化测试 PDF 下载深入理解Android自动化测试 PDF 下载
androidandroid自动化测试androidandroid自动化测试androidandroid自动化测试androidandroid自动化测试androidandroid自动化测试androidandroid自动化测试
android自动化测试android自动化测试
非扫描版 带书签许奔 著ISBN:978-7-111-52120-4目录本书赞誉前言第一部分 基础篇第1章 Android自动化测试基础1.1 Android自动化分析1.2 什么样的兵器最称手1.3 如何耍好你手上的兵器1.4 你了解你的兵器吗1.5...
3.Create_Android_Sample_...Android自动化测试初探 Android自动化测试可行性途径分析 Android自动化测试之道 GSM手机自动化测试解决方案 手机自动化测试系统设计浅析 在Android_Robotium自动化测试中导出Test_result
腾讯Android自动化测试实战电子书,针对移动安卓进行自动化测试
Android自动化测试框架
Cafe 测试框架是一款来自百度QA部门的具有开创性意义的Android平台的自动化测试框架,框架覆盖了Android自动化测试的各种需求。框架致力于实现跨进程测试、快速测试、深度测试,解决了Android自动化测试中的诸多难题...
Android自动化测试之Robotium学习
Android自动化测试总结,对android自动化测试的一些工具的使用总结
腾讯Android自动化测试实战(腾讯制作)
这是我收集的android自动化测试— 有关于robotium的一些资料 包括自己整理的一些文档 共4篇。。 jar也包括在内。 最重要的是还有一个Demo程序包,其中包括两个程序,一个测试程序一个被测程序。。 5分绝对值得的,...
许奔-深入理解Android自动化测试(带书签完整版)
深入理解Android自动化测试 (移动开发) - 许奔 著.mobi
Android UiAutomator 自动化测试原理 1、Android自动化工具简介 2、环境安装配置 3、测试原理及案例 4、其它注意事项 Android官方自动化测试工具 基于python Monkey是Android SDK自带的测试工具 MonkeyRunner也是...
Android自动化测试(MonkeyRunner)脚本
Android自动化测试工具,从连接app到自动测试界面控件元素,非常详细!官网例子
最近研究了一下android的自动化测试工具,其中用的比较多的就是google开源自带的monkeyrunner,还有一个就是robotium工具。文档是在eclipse中添加python和robotium插件的配置环境说明,方便使用monkeyrunner或是...
Android 自动化测试框架 一、Monkey 是Android SDK自带的测试工具,是一个命令行工具,可以运行在模拟器中或者实际设备中,它向系统发送伪随机的用户事件流(如按键输入,触摸屏输入,手势输入等),实现对正在...
旨在讲解Android自动化测试的探索、实施和经验分享。