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();
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341