LoginSignup
0
1

More than 5 years have passed since last update.

Android StudioでEmulatorを起動しないUnit test

Posted at

開発環境

  • Android Studio 2.3
  • JRE 1.8.0

Android studio プロジェクトの中に androidTestとは別、test だけのパッケージがあります。その中に ExampleUnitTest ユニットテストクラスが入っています。ユニットテストをEmulatorを介さずJavaでデバッグする設置をまとめました。

まず、Runボタンの隣のプルダウンメニューを開き、Edit Configurationを選択。

3月-24-2017 17-34-49.gif

その次に、左上のプラスボタンで、Moduleを設定し、ExampleUnitTestクラスがユニットテストする対象と設定し、デバッグ方法を追加します。PS: Test kind の中に、Class単体テストのか、package、directory単位でテストするのかを選択できます、今回はClass単体テストで進みます。

3月-24-2017 17-38-57.gif

Runボタンの隣に先ほど追加した Unnamed になるので、 Runボタンを押して実行します。

image

実行したら、テストの結果が下のコンソールに表示されます。

3月-24-2017 17-42-09.gif

補足: テストファイルを新規追加した議事録

3年前にEclipseで作ったAndroidプロジェクト、 Android Studio v2.3の時にプロジェクトをインポートしたらテストフォルダがなくて、追加した議事録になります。参考になればと思います。

まず、左メニュー、ファイルツリーが表示されているところをAndroidからProjectに変更します。
image

srcフォルダにtestフォルダを追加し、パッケージとユニットテストのクラスファイルを追加します。
image

最後はbuild.gradleファイルにjunitとcom.android.support.testを追加し、syncします。

build.gradle

...

dependencies {
    compile 'com.google.android.gms:play-services:10.0.1'

    ...

    // junitとcom.android.support.testを追加します
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    testCompile 'junit:junit:4.12'
}
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1