首页 > 代码库 > Android测试框架初步

Android测试框架初步

一、实验目的

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="http://www.mamicode.com/@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测试框架初步