• Android Video Tutorials

Android - 測試



Android 框架包含一個整合的測試框架,可幫助您測試應用程式的所有方面,並且 SDK 工具包含用於設定和執行測試應用程式的工具。無論您是在使用 ADT 的 Eclipse 中工作,還是在命令列中工作,SDK 工具都可幫助您在模擬器或您要定位的裝置中設定和執行測試。

測試結構

Android 的構建和測試工具假設測試專案被組織成一個標準的測試、測試用例類、測試包和測試專案的結構。

Android Testing Tutorial

Android 中的測試工具

有許多工具可用於測試 Android 應用程式。有些是官方的,如 Junit、Monkey,還有一些是第三方工具,可用於測試 Android 應用程式。在本章中,我們將解釋這兩個用於測試 Android 應用程式的工具。

  • JUnit
  • Monkey

JUnit

您可以使用 JUnit 的 **TestCase** 類對不呼叫 Android API 的類進行單元測試。TestCase 也是 AndroidTestCase 的基類,您可以使用它來測試依賴於 Android 的物件。除了提供 JUnit 框架之外,AndroidTestCase 還提供了 Android 特定的設定、拆卸和輔助方法。

為了使用 TestCase,請使用 TestCase 類擴充套件您的類並實現一個名為 setUp() 的方法呼叫。其語法如下所示:

public class MathTest extends TestCase {
   protected double fValue1;
   protected double fValue2;

   protected void setUp() {
      fValue1= 2.0;
      fValue2= 3.0;
   }
}

對於每個測試,實現一個與夾具互動的方法。使用斷言(透過呼叫 assertTrue(String, boolean) 並傳入一個布林值)來驗證預期結果。

public void testAdd() {
   double result= fValue1 + fValue2;
   assertTrue(result == 5.0);
}

斷言方法將您期望從測試中獲得的值與實際結果進行比較,如果比較失敗則丟擲異常。

定義完方法後,您可以執行它們。其語法如下所示:

TestCase test= new MathTest("testAdd");
test.run();

Monkey

UI/應用程式執行程式 Monkey,通常稱為“monkey”,是一個命令列工具,它向裝置傳送偽隨機的按鍵、觸控和手勢流。您可以使用 Android 除錯橋 (adb) 工具執行它。

您可以使用它對您的應用程式進行壓力測試並報告遇到的錯誤。您可以透過每次使用相同的隨機數種子執行該工具來重複事件流。

Monkey 功能

Monkey 有許多功能,但它們都可以歸納為以下四大類。

  • 基本配置選項
  • 操作約束
  • 事件型別和頻率
  • 除錯選項

Monkey 用法

要使用 monkey,請開啟命令提示符並導航到以下目錄。

android ->sdk ->platform-tools

進入目錄後,將您的裝置連線到 PC,並執行以下命令。

adb shell monkey -p your.package.name -v 500

此命令可以分解成以下步驟。

  • adb - Android 除錯橋。用於連線並向您的 Android 手機發送命令的工具,來自桌面或筆記型電腦。
  • shell - shell 只是裝置上一個將我們的命令轉換為系統命令的介面。
  • monkey - monkey 是測試工具。
  • v - v 代表詳細方法。
  • 500 - 它是頻率計數或要傳送的測試事件數。

這也在圖中顯示 -

Android Testing Tutorial

在上面的命令中,您在預設的 Android UI 應用程式上執行 monkey 工具。現在,要將其執行到您的應用程式,您需要執行以下操作。

最後,您將獲得如下所示的完成提示

這也在下圖中顯示。透過鍵入此命令,您實際上是在生成 500 個隨機事件進行測試。

Android Testing Tutorial

示例

以下示例演示了測試的使用。它建立一個可用於 monkey 的基本應用程式。

要試驗此示例,您需要在實際裝置上執行它,然後按照開頭解釋的 monkey 步驟操作。

步驟 描述
1 您將使用 Android Studio 在包 com.tutorialspoint.myapplication 下建立一個 Android 應用程式。
2 修改 src/MainActivity.java 檔案以新增 Activity 程式碼。
3 修改佈局 XML 檔案 res/layout/activity_main.xml,如果需要,新增任何 GUI 元件。
4 建立 src/second.java 檔案以新增 Activity 程式碼。
5 修改佈局 XML 檔案 res/layout/view.xml,如果需要,新增任何 GUI 元件。
6 執行應用程式並選擇一個正在執行的 Android 裝置,將應用程式安裝在其上並驗證結果。

以下是 **MainActivity.java** 的內容。

package com.tutorialspoint.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
   Button b1;
   @Override

   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      b1=(Button)findViewById(R.id.button);
   }

   public void button(View v){
      Intent in =new Intent(MainActivity.this,second.class);
      startActivity(in);
   }

}

以下是 **second.java** 的內容。

package com.tutorialspoint.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class second extends Activity{
   @Override

   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.view);
      Button b1=(Button)findViewById(R.id.button2);

      b1.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            Toast.makeText(second.this,"Thanks",Toast.LENGTH_SHORT).show();
         }
      });
   }
}

以下是 **activity_main.xml** 的內容。

在下面的程式碼中,**abc** 表示 tutorialspoint.com 的徽標
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
   android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
   
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="UI Animator Viewer"
      android:id="@+id/textView"
      android:textSize="25sp"
      android:layout_centerHorizontal="true" />
      
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Tutorials point"
      android:id="@+id/textView2"
      android:layout_below="@+id/textView"
      android:layout_alignRight="@+id/textView"
      android:layout_alignEnd="@+id/textView"
      android:textColor="#ff36ff15"
      android:textIsSelectable="false"
      android:textSize="35dp" />
      
   <ImageView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/imageView"
      android:src="@drawable/abc"
      android:layout_below="@+id/textView2"
      android:layout_centerHorizontal="true" />
      
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Button"
      android:onClick="button"
      android:id="@+id/button"
      android:layout_below="@+id/imageView"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="100dp" />

</RelativeLayout>

以下是 **view.xml** 的內容

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent" android:layout_height="match_parent">
   
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="button"
      android:id="@+id/button2"
      android:layout_centerVertical="true"
      android:layout_centerHorizontal="true" />
      
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Tutorials point "
      android:id="@+id/textView3"
      android:textColor="#ff3aff22"
      android:textSize="35dp"
      android:layout_above="@+id/button2"
      android:layout_centerHorizontal="true"
      android:layout_marginBottom="90dp" />
      
</RelativeLayout>

以下是 **Strings.xml** 的內容。

<resources>
   <string name="app_name">My Application</string>
</resources>

以下是 **AndroidManifest.xml** 的內容。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.tutorialspoint.myapplication" >
   
   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      
      <activity
         android:name=".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>
      
      <activity android:name=".second"></activity>
      
   </application>
</manifest>

讓我們嘗試執行您的 Android 測試應用程式。我假設您已將您的實際 Android 移動裝置連線到您的計算機。要從 Android Studio 執行該應用程式,請開啟專案的某個 Activity 檔案,然後單擊工具欄中的執行 Eclipse Run Icon 圖示。在啟動應用程式之前,Android Studio 將顯示以下視窗以選擇要在其中執行 Android 應用程式的選項。

Anroid Testing Tutorial

選擇您的移動裝置作為選項,然後檢查您的移動裝置,它將顯示應用程式螢幕。現在,只需按照 monkey 部分頂部提到的步驟操作,即可在此應用程式上執行測試。

廣告

© . All rights reserved.