一、实验目的

1.掌握android测试项目的建立

2.掌握android测试框架的基本内容

3.编写运行android测试

二、实验内容与步骤

建立android项目MyProject,运行截图如下:

l  点击ok按钮,EditText内字母变大写

l  点击超链接,打开浏览器上网

请用知识对本项目进行测试,要求:

1、对组件进行对齐测试(assertOnScreen和assertRightAligned方法)

2、对EditText进行传值测试(使用sendKeys 和 sendRepeatedKeys两种方法)

3、对Button进行功能测试(performClick和sendKeys方法)

4、对超链接进行测试(ActivityMonitor内部类)

5、为了保证测试的完整性和准确性,请适当添加必要的功能(如先决条件,多种方法等)

注:建立android测试项目过程如下

1、新建android测试项目

2、建立好测试项目之后,在测试项目中的src目录下,右键点击你创建的包,依次选择新建—>JUnit Test Case,弹出如下界面:

 
//代码
package com.sise.zhw;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText; public class MyProjectActivity extends Activity {
/** Called when the activity is first created. */
private EditText mMessage;
private Button mok;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMessage=(EditText)findViewById(R.id.message);
mok=(Button)findViewById(R.id.ok);
mok.setOnClickListener(new OnClickListener()
{ public void onClick(View v)
{
// TODO Auto-generated method stub
mMessage.setText(mMessage.getText().
toString().toUpperCase());
}
});
}
}

//布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:layout_width="wrap_content"
android:id="@+id/imageView1"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:layout_marginBottom="6dip"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dip"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MyFirstProjectTest"
android:layout_gravity="center"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="www.sise.com.cn"
android:layout_gravity="center"
android:autoLink="web"
android:id="@+id/link"
android:textSize="18sp"
/>
<EditText
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginBottom="0dip"
android:layout_marginLeft="6dip"
android:layout_marginRight="6dip"
android:layout_marginTop="24dip"
android:hint="sise"
android:id="@+id/message"
android:maxLines="1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_margin="6dip"
android:paddingLeft="24dip"
android:paddingRight="24dip"
android:text="ok"
android:id="@+id/ok"
/>
</LinearLayout>

  

//测试代码
package com.sise.zhw.test;

import static android.test.MoreAsserts.assertNotContainsRegex;
import static android.test.ViewAsserts.assertOnScreen;
import static android.test.ViewAsserts.assertRightAligned; import com.sise.zhw.MyProjectActivity; import android.app.Instrumentation;
import android.app.Instrumentation.ActivityMonitor;
import android.content.Intent;
import android.content.IntentFilter;
import android.test.ActivityInstrumentationTestCase2;
import android.test.TouchUtils;
import android.test.UiThreadTest;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView; public class MyFirstProjectActivityTests extends
ActivityInstrumentationTestCase2<MyProjectActivity> {
private MyProjectActivity mActivity;
private EditText mMessage;
private Button mCapitalize;
private TextView mLink;
private Instrumentation mInstrumentation;
public MyFirstProjectActivityTests() {
this("MyFirstProjectActivityTests");
}
public MyFirstProjectActivityTests(String name) {
super(MyProjectActivity.class);
setName(name);
}
protected void setUp() throws Exception {
super.setUp();
setActivityInitialTouchMode(false);
mActivity = getActivity();
mInstrumentation = getInstrumentation();
mLink = (TextView)mActivity.findViewById(com.sise.zhw.R.id.link);
mMessage = (EditText)mActivity.findViewById(com.sise.zhw.R.id.message);
mCapitalize = (Button)mActivity.findViewById(com.sise.zhw.R.id.ok);
}
protected void tearDown() throws Exception {
super.tearDown();
}
public void testPreConditions() {
assertNotNull(mActivity);
assertNotNull(mInstrumentation);
assertNotNull(mLink);
assertNotNull(mMessage);
assertNotNull(mCapitalize);
}
public void testAlignment() {
final int margin = 0;
assertRightAligned(mMessage, mCapitalize, margin);
}
public void testUserInterfaceLayout() {
final int margin = 0;
final View origin = mActivity.getWindow().getDecorView();
assertOnScreen(origin, mMessage);
assertOnScreen(origin, mCapitalize);
assertRightAligned(mMessage, mCapitalize, margin);
}
public void testUserInterfaceLayoutWithOtherOrigin() {
final int margin = 0;
View origin = mMessage.getRootView();
assertOnScreen(origin, mMessage);
origin = mCapitalize.getRootView();
assertOnScreen(origin, mCapitalize);
assertRightAligned(mMessage, mCapitalize, margin);
}
@UiThreadTest
public void testNoErrorInCapitalization() {
final String msg = "this is a sample";
mMessage.setText(msg);
mCapitalize.performClick();
final String actual = mMessage.getText().toString();
final String notExpectedRegexp = "(?i:ERROR)";
assertNotContainsRegex("Capitalization found error:",
notExpectedRegexp, actual);
}
public void testFollowLink() {
final Instrumentation inst = getInstrumentation();
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_VIEW);
intentFilter.addDataScheme("http");
intentFilter.addCategory(Intent.CATEGORY_BROWSABLE);
ActivityMonitor monitor = inst.addMonitor(intentFilter, null, false);
assertEquals(0, monitor.getHits());
TouchUtils.clickView(this, mLink);
monitor.waitForActivityWithTimeout(5000);
assertEquals(1, monitor.getHits());
inst.removeMonitor(monitor);
} private void requestMessageFocus() {
try {
runTestOnUiThread(new Runnable() {
public void run() {
mMessage.requestFocus();
}
});
} catch (Throwable e) {
fail("Couldn't set focus");
}
mInstrumentation.waitForIdleSync();
} public void testSendKeyInts() {
requestMessageFocus();
sendKeys(KeyEvent.KEYCODE_H,
KeyEvent.KEYCODE_E,
KeyEvent.KEYCODE_E,
KeyEvent.KEYCODE_E,
KeyEvent.KEYCODE_Y,
KeyEvent.KEYCODE_ALT_LEFT,
KeyEvent.KEYCODE_1,
KeyEvent.KEYCODE_DPAD_DOWN,
KeyEvent.KEYCODE_ENTER);
final String expected = "HEEEY!";
final String actual = mMessage.getText().toString();
assertEquals(expected, actual);
} public void testSendKeyString() {
requestMessageFocus();
sendKeys("H 3*E Y ALT_LEFT 1 DPAD_DOWN ENTER");
final String expected = "HEEEY!";
final String actual = mMessage.getText().toString();
assertEquals(expected, actual);
} public void testSendRepeatedKeys() {
requestMessageFocus();
sendRepeatedKeys(1, KeyEvent.KEYCODE_H,
3, KeyEvent.KEYCODE_E,
1, KeyEvent.KEYCODE_Y,
1, KeyEvent.KEYCODE_ALT_LEFT,
1, KeyEvent.KEYCODE_1,
1, KeyEvent.KEYCODE_DPAD_DOWN,
1, KeyEvent.KEYCODE_ENTER); final String expected = "HEEEY!";
final String actual = mMessage.getText().toString();
assertEquals(expected, actual);
} public void testCapitalizationSendingKeys() {
final String keysSequence = "T E S T SPACE M E";
requestMessageFocus();
sendKeys(keysSequence);
TouchUtils.clickView(this, mCapitalize);
final String expected = "test me".toUpperCase();
final String actual = mMessage.getText().toString();
assertEquals(expected, actual);
} public void testActivityPermission(){
final String PKG="com.sise.zhw";
final String ACTIVITY=PKG+".MyContactsActivity";
final String PERMISSION="android.MainFest.permission.CALL_PHONE";
//assertActivityRequiresPermission(PKG,ACTIVITY,PERMISSION); } }

  

Android测试框架初步的更多相关文章

  1. 5个最佳的Android测试框架(带示例)

    谷歌的Android生态系统正在不断地迅速扩张.有证据表明,新的移动OEM正在攻陷世界的每一个角落,不同的屏幕尺寸.ROM /固件.芯片组以及等等等等,层出不穷.于是乎,对于Android开发人员而言 ...

  2. Android测试框架1(非原创)

    1.继承AndroidTestCase :public class JunitTest3 extends AndroidTestCase {} 2.在AndroidManifest.xml清单文件中添 ...

  3. Android测试框架-uiautomator

    官方示例:https://github.com/googlesamples/android-testing 官方文档请 google 要求: Android SDK v23 Android Build ...

  4. Android测试框架2(非原创)

    package com.example.sqlitedatabase.test; import android.content.ContentValues;import android.databas ...

  5. Android开源测试框架学习

    近期因工作需要,分析了一些Android的测试框架,在这也分享下整理完的资料. Android测试大致分三大块: 代码层测试 用户操作模拟,功能测试 安装部署及稳定性测试 代码层测试 对于一般java ...

  6. [转]Android开源测试框架学习

    近期因工作需要,分析了一些Android的测试框架,在这也分享下整理完的资料. Android测试大致分三大块: 代码层测试 用户操作模拟,功能测试 安装部署及稳定性测试 代码层测试 对于一般java ...

  7. android测试分析1

    Android测试框架,开发环境中集成的一部分,提供一个架构和强有力的工具 可以帮助测试你的应用从单元到框架的每个方面. 测试框架有这些主要特征: 1.Android测试组件基于Junit.你可以使用 ...

  8. 2014 非常好用的开源 Android 测试工具

    http://www.php100.com/html/it/mobile/2014/1015/7495.html 当前有很大的趋势是转向移动应用平台,Android 是最广泛使用的移动操作系统,201 ...

  9. Android Junit测试框架

    对应用进行单元测试: 使用Junit测试框架,是正规Android开发的必用技术.在Junit中可以得到组件,可以模拟发送事件和检测程序处理的正确性. 1.配置指令集和函数库: (1)配置指令集,指定 ...

随机推荐

  1. 关于Redis info的参数总结

    Redis官网对 info 已经讲解的比较清楚的,参考文档 .可以看到,info的输出结果是分几块的,有Servers.Clients.Memory等等,通过info后面接这些参数,可以指定输出某一块 ...

  2. 常用的Linux可插拔认证模块(PAM)应用举例(一)

    pam_access.so模块 pam_access.so模块主要的功能和作用是根据主机名(包括普通主机名或者FQDN).IP地址和用户实现全面的访问控制.pam_access.so模块的具体工作行为 ...

  3. SOS: gnuplot fdtd的一个问题求助 perl vs python

    我用perl和python写了相同功能的一段程序,计算一维fdtd,用gnuplot动态显示,可是python的数据没有显示出来,看横纵坐标的变化数据是正确收到了的,如最后的图片,求大神指点,谢谢. ...

  4. selenium如何分别启动IE、firefox、chrome浏览器

    1.火狐浏览器 /* * 初始化火狐浏览器 * */ public static WebDriver initFireFox(WebDriver dr) { String key = "we ...

  5. 敬爱的GitHub” —— 致GitHub的一封地下信   英文原文:"Dear GitHub…" An Open Letter to GitHub

    敬爱的GitHub” —— 致GitHub的一封地下信 英文原文:"Dear GitHub…" An Open Letter to GitHub 最近,一个由开源名目(包含一些最盛 ...

  6. hihocoder-1389&&2016北京网赛07 Sewage Treatment(二分+网络流)

    题目链接: Sewage Treatment 时间限制:2000ms 单点时限:2000ms 内存限制:256MB 描述 After years of suffering, people could ...

  7. POJ 1703 Find them,Catch them ----种类并查集(经典)

    http://blog.csdn.net/freezhanacmore/article/details/8774033?reload  这篇讲解非常好,我也是受这篇文章的启发才做出来的. 代码: #i ...

  8. 颗粒翻页(css3效果展示)

    用css3效果做了一个颗粒翻页效果,布局上,一张图片做底层,在这张图片上用js创建一层小的行和列各为r和c的小span,给这些span分别设置background-position:用来覆盖原来的一张 ...

  9. Sublime Text2 新建文件快速生成Html头部信息和炫酷的代码补全

    预备:安装emmet插件(previously known as Zen Coding) 方法一  package control法: 上一篇博客已经介绍了如何安装package control.打开 ...

  10. javascript:让表单 文本框 只读,不可编辑的方法

    有时候,我们希望表单中的文本框是只读的,让用户不能修改其中的信息,如使<input type="text" name="input1" value=&qu ...