Android 測試支援庫介紹

來源:互聯網
上載者:User

標籤:

測試支援庫

Android的測試支援庫為測試Android應用提供了大量架構。該庫提供了一組API快速構建和運行測試代碼,包括JUnit4和功能使用者介面(UI)測試。可以從Android Studio IDE中或命令列這執行。

Android的測試支援庫可通過Android SDK管理器擷取。

測試支援庫特性

AndroidJUnitRunner:相容JUnit 4測試回合器。 Espresso:UI測試架構;適合在單個應用的功能UI測試。 UI Automator:UI測試架構;適用於跨應用的功能UI測試及安裝應用。

AndroidJUnitRunner

AndroidJUnitRunner類是JUnit測試回合器,可以讓你在Android裝置上執行JUnit3或JUnit4中風格的測試類別,相容Espresso和UI Automator測試架構。測試回合器載入測試包和應用,運行測試並報告測試結果。該類取代 InstrumentationTestRunner類(僅支援JUnit 3)。

這個運行器的主要特點:

  • JUnit支援

  • 獲得Instrumentation資訊

  • 測試篩選

  • 測試分區

要求的Android2.2(API 8)或更高。

 

測試回合器相容JUnit3和JUnit4的(最高JUnit4.10)測試。在同一個包混淆JUnit 3和和JUnit4測試代碼可能會導致不可預測的結果。instrumented JUnit 4測試類別在裝置或模擬器上運行,必須在前面加上@RunWith(AndroidJUnit4.class)注釋。

 

比如測試CalculatorActivity類中的加操作:

 

import android.support.test.runner.AndroidJUnit4;import android.support.test.runner.AndroidJUnitRunner;import android.test.ActivityInstrumentationTestCase2;@RunWith(AndroidJUnit4.class)public class CalculatorInstrumentationTest        extends ActivityInstrumentationTestCase2<CalculatorActivity> {    @Before    public void setUp() throws Exception {        super.setUp();        // Injecting the Instrumentation instance is required        // for your test to run with AndroidJUnitRunner.        injectInstrumentation(InstrumentationRegistry.getInstrumentation());        mActivity = getActivity();    }    @Test    public void typeOperandsAndPerformAddOperation() {        // Call the CalculatorActivity add() method and pass in some operand values, then        // check that the expected value is returned.    }    @After    public void tearDown() throws Exception {        super.tearDown();    }}

InstrumentationRegistry類可以訪問測試回合的資訊。該類包括Instrumentation對象,目標應用內容物件,測試應用內容物件及傳遞到測試的命令列參數。

JUnit 4.x的測試可以使用annotation來配置測試回合,並支援Androidannotation:

@RequiresDevice:物理裝置上運行。

@SdkSupress:限定最低SDK版本。例如@SDKSupress(minSdkVersion=18)
@SmallTest,@MediumTest和@LargeTest:測試分級。

單個test suite可以分區,同一Instrumentation的同一分區可以作為一個組。每個片都有索引號。當運行測試,使用-e numShards選項指定片數和-e shardIndex選項來指定要啟動並執行片。

例如分成10個片段,僅執行第二片測試,請使用以下命令:

adb shell am instrument -w -e numShards 10 -e shardIndex 2
Espresso

Espresso提供了一組API來構建UI測試來測試使用者流程。這些API讓你寫簡潔和可靠啟動並執行自動化UI測試。Espresso非常適合白盒自動化的測試,測試代碼利用了被測應用的代碼細節。

Espresso的主要特性:

  • 視圖匹配(View matching): 靈活的API用於查看和適配目標應用。

  • Action API:一套擴充的action API自動化UI互動。

  • UI線程同步(UI thread synchronization)以提高測試的可靠性。

要求Android2.2(API 8)或更高。

Espresso.onView()方法可以訪問目標應用程式的UI組件並與之互動。該方法接受一個Matcher參數,搜尋視圖層來定位視圖執行個體。定位方法可以基於類名、內容描述、R.id、顯示的文本。比如

onView(withId(R.id.my_button));


如果搜尋成功,onView()方法返回對應view的引用,可執行使用者操作和斷言。

AdapterView由子view動態產生。如果目標視圖在AdapterView(ListView或GridView)中,onView()方法可能不起作用,因為可能只載入了一部分,Espresso.onData()則可以。

ViewActions可以執行視圖點擊(View clicks),滑動(Swipes),按鍵或者按鈕(Key and button press)、文本輸入(Typing text)、開啟連結(Opening a link)。

// Type text into an EditText view, then close the soft keyboardonView(withId(R.id.editTextUserInput))    .perform(typeText(STRING_TO_BE_TYPED), closeSoftKeyboard());// Press the button to submit the text changeonView(withId(R.id.changeTextBt)).perform(click());

由於時間問題,在Android裝置上測試隨機失敗。之前一般通過sleep和逾時處理解決。Espresso測試架構處理Instrumentation和UI線程之間的同步,很好地解決了這些問題。

API參考:developer.android.com/reference/android/support/test/package-summary.html
測試參考:http://developer.android.com/training/testing/ui-testing/espresso-testing.html

 

UI Automator

UI Automator提供了一組API來構建基於互動UI的測試。API允許你執行操作,如開啟設定菜單,非常適合黑盒自動化測試,測試代碼不依賴於應用的內部實現。

主要特性:

  • UI Automator Viewer:檢查的布局層次。

  • API來擷取裝置狀態資訊並執行操作。

  • API跨應用測試。

要求Android4.3(API等級18)或者更高。

uiautomatorviewer提供了一個方便的圖形化使用者介面進行掃描和分析在Android裝置上當前顯示的UI組件。您可以使用此工具來檢查的布局層次和查看UI組件。

UiDevice類可以訪問裝置並進行操作。你可以調用它的方法來訪問裝置屬性,如當前的方向或顯示尺寸。該UiDevice類也讓您執行操作,例如:旋轉裝置;按下D-pad按鈕;按Back、Home、Menu等;開啟通知樹欄;當前視窗等。比如按Home鍵:UiDevice.pressHome()。

更多應用相關的API: UiCollection枚舉容器的UI元素以計數,或通過文字(或屬性等)定位子項目; UIObject表示是在裝置上可見的UI元素; UiScrollable:為可滾動UI容器提供尋找支援; UiSelector:查詢一個或者多個UI元素; Configurator: 設定參數。比如:

// Initialize UiDevice instancemDevice = UiDevice.getInstance(getInstrumentation());// Perform a short press on the HOME buttonmDevice().pressHome();// Bring up the default launcher by searching for// a UI component that matches the content-description for the launcher buttonUiObject allAppsButton = mDevice        .findObject(new UiSelector().description("Apps"));// Perform a click on the button to bring up the launcherallAppsButton.clickAndWaitForNewWindow();

API參考:http://developer.android.com/reference/android/support/test/package-summary.html
執行個體:http://developer.android.com/training/testing/ui-testing/uiautomator-testing.html

https://pypi.python.org/pypi/uiautomator 用python封裝了uiautomator,操作起來更簡單,推薦使用。

 

測試支援庫安裝
android {    defaultConfig {        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"    }}

 

  • 啟動Android SDK管理器。

  • 在SDK管理器視窗,滾動到軟體包列表的末尾,找到其他檔案夾,如有必要,展開以顯示其內容。

  • 選擇 Android Support Repository。

  • 點擊Install packages。

    下載後安裝支援庫檔案到您現有的Android SDK目錄。該庫檔案位於你的SDK的子目錄:<sdk>/extras/android/m2repository。

    Android的測試支援庫位於android.support.test包。

    Gradle中使用:build.gradle檔案中添加這些依賴關係:

    dependencies {  androidTestCompile ‘com.android.support.test:runner:0.4‘  // Set this dependency to use JUnit 4 rules  androidTestCompile ‘com.android.support.test:rules:0.4‘  // Set this dependency to build and run Espresso tests  androidTestCompile ‘com.android.support.test.espresso:espresso-core:2.2.1‘  // Set this dependency to build and run UI Automator tests  androidTestCompile ‘com.android.support.test.uiautomator:uiautomator-v18:2.1.2‘}


    設定AndroidJUnitRunner為預設測試:
    強烈建議您一起使用Android測試支援庫與Android Studio IDE。 Android的Studio支援測試開發,如:

        基於Gradle的靈活的構建系統,支援測試代碼的依賴管理。
        單個項目包含應用程式原始碼和測試代碼。
        支援虛擬或物理裝置的部署和運行測試,支援命令列或圖形化使用者介面

    更多介紹參見:http://developer.android.com/sdk/index.html

參考資料
  • Testing Support Library

 

Android 測試支援庫介紹

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.