Android-testing

提供:Dev Guides
移動先:案内検索

Android-テスト

Androidフレームワークには、アプリケーションのすべての側面をテストするのに役立つ統合テストフレームワークが含まれ、SDKツールには、テストアプリケーションをセットアップおよび実行するためのツールが含まれています。 EclipseでADTを使用して作業している場合でも、コマンドラインから作業している場合でも、SDKツールを使用すると、エミュレータまたはターゲットのデバイス内でテストをセットアップして実行できます。

テスト構造

Androidのビルドおよびテストツールは、テストプロジェクトがテスト、テストケースクラス、テストパッケージ、およびテストプロジェクトの標準構造に編成されていることを前提としています。

Androidテストチュートリアル

Androidのテストツール

Androidアプリケーションのテストに使用できる多くのツールがあります。 Junit、Monkeyのような公式のものもあれば、Androidアプリケーションのテストに使用できるサードパーティ製ツールもあります。 この章では、Androidアプリケーションをテストするこれら2つのツールについて説明します。

  • JUnit *モンキー

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);
}

assertメソッドは、テストから期待される値を実際の結果と比較し、比較が失敗した場合に例外をスローします。

メソッドが定義されると、それらを実行できます。 その構文は以下のとおりです-

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

モンキー

UI/アプリケーションエクササイザーモンキーは、通常「モンキー」と呼ばれ、キーストローク、タッチ、ジェスチャーの擬似ランダムストリームをデバイスに送信するコマンドラインツールです。 Android Debug Bridge(adb)ツールを使用して実行します。

これを使用して、アプリケーションのストレステストを行い、発生したエラーを報告します。 同じ乱数シードを使用して毎回ツールを実行することにより、イベントのストリームを繰り返すことができます。

猿の特徴

Monkeyには多くの機能がありますが、これらはすべてこれら4つのカテゴリーにまとめることができます。

  • 基本設定オプション
  • 運用上の制約
  • イベントの種類と頻度
  • デバッグオプション

猿の使い方

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-テストのために送信される頻度conutまたはイベントの数です。

これも図に示されています-

Androidテストチュートリアル

上記のコマンドでは、デフォルトのAndroid UIアプリケーションでmonkeyツールを実行します。 これをアプリケーションに実行するために、ここで何をする必要があります。

最後に、以下に示すように終了します

これは、次の図にも示されています。 このコマンドを入力すると、実際にはテスト用に500個のランダムイベントが生成されます。

Androidテストチュートリアル

次の例は、テストの使用方法を示しています。 サルに使用できる基本的なアプリケーションを作成します。

この例を試すには、これを実際のデバイスで実行してから、最初に説明した猿の手順に従う必要があります。

Steps Description
1 You will useAndroid studio to create an Android application under a package com.finddevguides.myapplication.
2 Modify src/MainActivity.java file to add Activity code.
3 Modify layouta XML file res/layout/activity_main.xml add any GUI component if required.
4 Create src/second.java file to add Activity code.
5 Modify layout XML file res/layout/view.xml add any GUI component if required.
6 Run the application and choose a running android device and install the application on it and verify the results.
*MainActivity.java* のコンテンツは次のとおりです。
package com.finddevguides.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.finddevguides.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 はfinddevguides.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.finddevguides.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スタジオからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[画像を実行:/android/images/eclipse_run.jpg [Eclipse Run Icon]アイコンをクリックします。 アプリケーションを開始する前に、Androidスタジオは次のウィンドウを表示して、Androidアプリケーションを実行するオプションを選択します。

Androidテストチュートリアル

オプションとしてモバイルデバイスを選択し、アプリケーション画面を表示するモバイルデバイスを確認します。 このアプリケーションでテストを実行するには、サルセクションの下にある上部の手順に従ってください。