天天看點

Android自動化測試中Espresso架構學習筆記(1)環境配置

環境配置

手機環境設定

首先在手機的開發者選項中關閉三個選項。由于我的手機開發者選項并不是預設顯示的,是以我先打開開發者選項。

進入設定->關于手機

Android自動化測試中Espresso架構學習筆記(1)環境配置

在版本号一欄輕按兩下出現提示。然後再按提示點選幾下。提示成功後,出現開發者選項。看一下前後對比:

Android自動化測試中Espresso架構學習筆記(1)環境配置
Android自動化測試中Espresso架構學習筆記(1)環境配置

開發者選項調出來後,進入裡面:

Android自動化測試中Espresso架構學習筆記(1)環境配置

将圖上的三項全部關閉。關閉後的樣子如下:

Android自動化測試中Espresso架構學習筆記(1)環境配置

Eclipse配置

首先建立一個普通的android應用Espresso。然後從espresso網站上下載下傳jar包,我下載下傳的我上傳到網站上來了。

http://code.google.com/p/android-test-kit/source/browse/#git%2Fbin

下載下傳完成以後,我們将其中的espresso-1.1-bundled.jar添加到android項目中。

Android自動化測試中Espresso架構學習筆記(1)環境配置

然後我們編寫具體代碼。在src下建立測試檔案夾,編寫測試用例。

Android自動化測試中Espresso架構學習筆記(1)環境配置

EspressoTest.java:

package com.ibm.espresso.test;


import com.google.android.apps.common.testing.ui.espresso.action.ViewActions;
import com.google.android.apps.common.testing.ui.espresso.assertion.ViewAssertions;
import com.google.android.apps.common.testing.ui.espresso.matcher.ViewMatchers;
import static com.google.android.apps.common.testing.ui.espresso.Espresso.onView;
import com.ibm.espresso.MainActivity;
import com.ibm.espresso.R;

import android.test.ActivityInstrumentationTestCase2;

@SuppressWarnings("rawtypes")
public class EspressoTest extends ActivityInstrumentationTestCase2 {
    @SuppressWarnings("unchecked")
    public EspressoTest() {
        super(MainActivity.class);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        getActivity();
    }

    public void testClickButton() throws InterruptedException {
        onView(ViewMatchers.withId(R.id.button1)).perform(ViewActions.click()).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
    }

    @Override
    protected void tearDown() throws Exception {
        super.tearDown();
    }
}
           

先不求具體意思,我們先讓一個完整的過程跑起來。是以上面的測試用例也是從網上找的。

參考文章:http://www.cnblogs.com/oscarxie/p/3521695.html

以上代碼完成後,我們就要配置測試環境啦。首先在AndroidManifest.xml檔案中做如下配置:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ibm.espresso"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <span style="color:#FF0000;"><uses-library android:name="android.test.runner" /></span>
        <activity
            android:name="com.ibm.espresso.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
	<span style="color:#FF0000;"><instrumentation
        android:name="com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
        android:targetPackage="com.ibm.espresso" /></span>
</manifest>
           

紅色标志的就是我們添加到2個标簽,一個是測試用的庫标簽,還有一個是<instrumentation>标簽。在<instrumentation>中的android:targetPackage屬性裡填的是被測試的主包名。

然後在項目名上右鍵選Run As->Run Configurations進入到如下畫面:

Android自動化測試中Espresso架構學習筆記(1)環境配置

輕按兩下Android Junit Test進入如下配置:

Android自動化測試中Espresso架構學習筆記(1)環境配置

配置完成後,我們點Run按鈕,會發現你的手機會自動安裝應用,然後點選。測試結果通過!

[2014-04-01 16:09:51 - Espresso] ------------------------------
[2014-04-01 16:09:51 - Espresso] Android Launch!
[2014-04-01 16:09:51 - Espresso] adb is running normally.
[2014-04-01 16:09:51 - Espresso] Performing com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner JUnit launch
[2014-04-01 16:09:51 - Espresso] Automatic Target Mode: using device '16442209010961'
[2014-04-01 16:09:51 - Espresso] Uploading Espresso.apk onto device '16442209010961'
[2014-04-01 16:09:51 - Espresso] Installing Espresso.apk...
[2014-04-01 16:09:57 - Espresso] Success!
[2014-04-01 16:09:57 - Espresso] Launching instrumentation com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner on 16442209010961
[2014-04-01 16:10:07 - Espresso] Sending test information to Eclipse
[2014-04-01 16:10:17 - Espresso] Test run finished
           

繼續閱讀