【Android測試】【第十五節】Instrumentation——官方譯文


版權聲明:本文出自胖喵~的博客,轉載必須注明出處。

   轉載請注明出處:http://www.cnblogs.com/by-dream/p/5482207.html

 

 

前言 


  前面介紹了不少Android UI自動化測試的東西,這里我們學習一下谷歌對安卓測試的一些理解。順便做為Instrumentation的預習篇。原文章的鏈接:http://developer.android.com/intl/zh-cn/tools/testing-support-library/index.html

  這篇文章介紹了Android App的關鍵概念。它假設你已經有了JUnit的測試框架的一些基本知識。

 

 

測試結構


  Android testing 測試框架是基於JUnit的。一般情況下,一個JUnit是一個方法 語句測試應用程序的一部分。你寫一些測試方法到一個類里,就被稱做是test case。當然你可進一步的組織這類到測試套件(test suites)。

  在JUnit當中,你建立一個或者多個測試類,並使用測試運行器(test runner)來執行它們。在Android中,你需要使用Android Studio(或 Android Plugin for Gradle)去建立一個或多個源文件到一個Android的測試App中。

  根據你的環境,你可以選擇以下方式之一運行測試:

  1、在你本地機器上:編譯測試類和使用JUnit test runner去調起他們執行在本地的JVM上。

  2、在模擬器或Android設備上:安裝測試程序到設備上,然后用Android特有的test runner(例如 AndroidJUnitRunner)去執行你的測試。

  你的測試代碼和你建立並運行Android Studio中的測試方式的結構取決於測試你正在執行的類型。下表總結了常見Android的測試類型的:

 

Type Subtype Description
Unit tests Local Unit Tests Unit tests that run on your local machine only. These tests are compiled to run locally on the JVM to minimize execution time. Use this approach to run unit tests that have no dependencies on the Android framework or have dependencies that mock objects can satisfy.
Instrumented unit tests Unit tests that run on an Android device or emulator. These tests have access toInstrumentation information, such as the Context of the app under test. Use this approach to run unit tests that have Android dependencies which mock objects cannot easily satisfy.
Integration Tests Components within your app only This type of test verifies that the target app behaves as expected when a user performs a specific action or enters a specific input in its activities. For example, it allows you to check that the target app returns the correct UI output in response to user interactions in the app’s activities. UI testing frameworks like Espresso allow you to programmatically simulate user actions and test complex intra-app user interactions.
Cross-app Components This type of test verifies the correct behavior of interactions between different user apps or between user apps and system apps. For example, you might want to test that your app behaves correctly when the user performs an action in the Android Settings menu. UI testing frameworks that support cross-app interactions, such as UI Automator, allow you to create tests for such scenarios.

  根據你創建的測試類型,你需要按照《Getting Started with Testing》中描述的在Android Studio中配置你測試代碼的路徑和項目依賴。

 

 

Testing APIs


  下面總結了Android測試相關的公共API。 

  Junit

    你在編寫單元測試或者集成測試類時需要把它作為Jnit 4的類, JUnit是Java中最流行和廣泛使用的單元測試框架。該框架提供了一個方便的方法去在你的應用中調用setup, teardown 和 assertion 。

    一個基本JUnit 4測試類是包含一個或多個Java測試類。一個測試方法是以一個@Test的標注開始,代碼的內容是就是驗證要測試組件的單一功能(也就是一個邏輯單元)。

    下面的代碼片段顯示了使用Espresso API來執行UI元素上點擊動作的JUnit 4集成測試的一個例子,這個例子是來檢查是否顯示了預期的字符串。

 1 @RunWith(AndroidJUnit4.class)
 2 @LargeTest
 3 public class MainActivityInstrumentationTest {
 4 
 5     @Rule
 6     public ActivityTestRule mActivityRule = new ActivityTestRule<>(
 7             MainActivity.class);
 8 
 9     @Test
10     public void sayHello(){
11         onView(withText("Say hello!")).perform(click());
12 
13         onView(withId(R.id.textView)).check(matches(withText("Hello, World!")));
14     }
15 }

    你可以使用Junit的Assert類來驗證對象狀態的正確性,通過斷言的方法來比較值,當實際結果與預期結果不一致的時候拋出異常。更多詳細的斷言內容可以參考Assertion classes(斷言類)。

 

  Instrumentation

    Android Instrumentation在安卓系統上是一組控制函數或者是hooks(鈎子)。這些鈎子在自己的生命周期獨立的控制一個安卓組件,他們也控制着安卓如何加載應用程序。

    下圖總結了Instrumentation的測試框架:

    通常情況下,Android的一個組件在運行在系統指定的生命周期中。舉個例子,一個Activity對象的生命周期開始就是被Intent激活的時候,系統調用該對象的onCreate()方法,然后調用onResume()方法,當用戶在切換到別的應用的時候,系統又調用onPause()方法,如果在Activity的代碼中調用finish()方法時,系統則會調用的onDestroy()方法。Android框架的API不提供對你的代碼直接調用這些回調函數,但你可以通過Instrumentation來完成。

    系統運行一個應用的所有組件都是在同一個進程中,你可以讓某些組件(例如content providers)在單獨的進程中運行,但是你不能強制讓一個應用程序和另一個已經運行的程序運行在同一個進程中。

    Instrumentation可以同時加載。一旦你的應用程序和你的測試程序在一個進程當中了,你的測試程序就可以調用組件中的方法,並且在組件中修改和驗證變量。

 

 

Android Testing Support Library APIs


  The Android Testing Support Library 提供了一系列的API,可以讓你快速的建立和運行你的測試程序,包括JUnit4和功能層面的用戶界面(UI)測試。下面這些庫都是基於Instrumentation的,你可以在做自動化測試的時候選擇它們:

  AndroidJUnitRunner:運行在安卓上的兼容JUnit 4的test runner;

  Espresso:UI測試框架,適用於在App內的UI功能測試;

  UIAutomator:UI測試框架,適用於跨應用的UI功能測試。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM