android jUnit test 进行自动化测试

一. 被test的工程:
新建一个android工程:D_session;它有一个activity:D_sessionActivity;package名:com.mysession
二.测试工程:
新建一个测试工程:D_sessionTest, 类型是android test project;
1. menifest文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mysession.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.mysession" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<uses-library android:name="android.test.runner" />
</application>
</manifest>
2. 一个base activity 来定义各种模拟测试者的动作和判断测试结果,各个测试类都继承该类:
package com.mysession.test;
import android.app.Activity;
import android.app.Instrumentation;
import android.app.Instrumentation.ActivityMonitor;
import android.content.Intent;
import android.test.InstrumentationTestCase;
import android.test.TouchUtils;
import android.widget.Button;
import android.widget.TextView;
import com.mysession.D_sessionActivity;
public class SessionActivityTest extends InstrumentationTestCase {
private Instrumentation mInstrumentation;
private ActivityMonitor mSessionMonitor;
private Activity mCurrentActivity, mSessionActivity;
private String TextNotEqual = "text not equal.";
private static final String PackageName = "com.mysession";
@Override
protected void setUp() throws Exception {
// 初始化
super.setUp();
if (mInstrumentation == null) {
mInstrumentation = getInstrumentation();
}
mSessionActivity = null;
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
//释放资源
closeActivity(mSessionActivity);
mCurrentActivity = null;
}
private void closeActivity(Activity activity) {
if(activity != null){
activity.finish();
activity = null;
}
}
public void openSessionActivity() {
// 打开session activity
try {
setUp();
} catch (Exception e) {
e.printStackTrace();
}
mSessionMonitor = mInstrumentation.addMonitor(
D_sessionActivity.class.getName(), null, false);
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setClassName(PackageName, D_sessionActivity.class.getName());
mInstrumentation.startActivitySync(intent);
mSessionActivity = getInstrumentation().waitForMonitor(mSessionMonitor);
assertNotNull(mSessionActivity);
mCurrentActivity = mSessionActivity;
}
//判断text是否正确
public void assertTextEqual(int resId, String strText) {
TextView textView = (TextView) mCurrentActivity.findViewById(resId);
assertNotNull(textView);
assertEquals(TextNotEqual,
strText,
textView.getText().toString());
};
// 模拟按钮点击事件
public void clickButton(int resId){
Button button = (Button) mCurrentActivity.findViewById(resId);
assertNotNull(button);
TouchUtils.clickView(this, button);
}
}
3. 各个测试类:
测试类一:
package com.mysession.test.cases;
import com.mysession.R;
import com.mysession.test.SessionActivityTest;
public class MyCase1 extends SessionActivityTest {
public void testCase1() {
openSessionActivity();
assertTextEqual(R.id.etUrl,
"http://172.20.230.5/iportal/samples/jsapi/mobile.html");
}
public void testCase3() {
openSessionActivity();
clickButton(R.id.btnLoad);
clickButton(R.id.btnHistory);
}
}
测试类二:
package com.mysession.test.cases;
import com.mysession.R;
import com.mysession.test.SessionActivityTest;
public class MyCase2 extends SessionActivityTest{
public void testCase2() {
openSessionActivity();
clickButton(R.id.btnLoad);
}
}
三. 有些动作(如点击menu)需要通过包robotium-solo-1.8.0.jar来完成。
所以要在Build Path->Configure Build Path…中导入:robotium-solo-1.8.0.jar
程序中:
private Solo solo;
solo = new Solo(getInstrumentation(),getActivity);
就可以使用solo了, 如:
solo.clickOnMenuItem(text);
solo.goBack();
摘自fhy_2008的专栏

android jUnit test 进行自动化测试,布布扣,bubuko.com

android jUnit test 进行自动化测试

上一篇:如何构建一个优秀的移动网站?谷歌专家教你25招[转]


下一篇:Mirror app HTML样式CSS之DIV遮罩背景层样式