您现在的位置是:首页 > 文章详情

Android JUnit test 进行自动化测试

日期:2017-07-02点击:290
一. 被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();


最新内容请见作者的GitHub页:http://qaseven.github.io/
原文链接:https://yq.aliyun.com/articles/130157
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章