一、实验目的

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. Android程序入口以及项目文件夹的含义和使用总结—入门

    新接触一门程序或者开发框架,我一般都要先弄清楚程序的入口在哪里,程序怎么运行的:建立一个项目后,各个文件夹有什么作用以及如何使用等等.理清楚这些东西对以后开发是很有好处的,古话说得好,工欲善其事,必先 ...

  2. 由IP和掩码计算广播地址

    public static IPAddress GetBroadcast(IPAddress ipAddress, IPAddress subnetMask) { var ip = ipAddress ...

  3. DirectX API 编程起步 #02 窗口的诞生

    在这篇文章里我们先用 windows API 制作一个窗口出来,以后再用 DirectX API 渲染的东西就会显示在这里,控制台那黑白的画面肯定是没法用的. 每次的代码都会更新到Github 首先贴 ...

  4. Scribefire离线编写博客的方法

    用Firefox下载Scribefire next插件www.scribefire.com cnblogs添加方法: URL:http://www.cnblogs.com/[你的博客名] API为ht ...

  5. volatile 关键字

    就象大家更熟悉的const一样,volatile是一个类型修饰符(type specifier).它是被设计用来修饰被不同线程访问和修改的变量.如果没有volatile,基本上会导致这样的结果:要么无 ...

  6. 使用flume的一个例子

    新项目中需要使用到hadoop和vertica,使用flume把数据加载到hadoop中,我做了一个例子, 即监控一个sharefolder,如果里面有文件,则会文件load到hadoop. 开启Fl ...

  7. SharpDX之Direct2D教程II——加载位图文件和保存位图文件

    本系列文章目录: SharpDX之Direct2D教程I——简单示例和Color(颜色) 绘制位图是绘制操作的不可缺少的一部分.在Direct2D中绘制位图,必须先利用WIC组件将位图加载到内存中,再 ...

  8. 【Javascript Demo】移动端访问PC端网页时跳转到对应的移动端网页

    不想通过CSS自适应在PC端和移动端分别显示不同的样式,那么只能通过在移动端访问PC端网页时跳转到对应的移动端网页了,那么怎么跳转呢,网上也有很多文章说明,下面是本人测试有效的方式. 1.效果图 PC ...

  9. 推荐——Monkey《大话 app 测试——Android、iOS 应用测试指南》

    <大话移动——Android与iOS应用测试指南> 京东可以预购啦!http://item.jd.com/11495028.html 当当网:http://product.dangdang ...

  10. web安全测试工具的局限性

    讨论安全漏洞的原理,谈谈工具的局限. 先说下扫描工具的原理: 扫描工具可以看做由两部分组成:爬虫+校验机构.爬虫的作用是搜集整个被采集对象的链接,然后校验机构对这些链接逐一进行验证. 说扫描工具的局限 ...