3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Android Studioのプロジェクトで複数のUnitTestを設定する(要Product Flavor)

Posted at

要約

フレーバー別にUnitTestを作れる。訳あって1つのアプリにテストが複数ある場合にも便利。

概要

Android Studio の特徴として Product Flavor があり、デバッグ向けとリリース向けのビルドを設定できるなど便利なのですが、UnitTestも別々に設定できます。

この場合、 build.gradle で androidTest とは別に、 androidTest+FlavorName なテストを設定します。ソースコードなどは src/ 以下に androidTest+FlavorName のディレクトリ名で置かれます(デフォルト値。setRootなどで変更可能)。

build.gradle

android {
    sourceSets {
        main {
            〜〜〜
        }
        androidTestFlavor1 {
            setRoot('androidTestFlavor1')
            〜〜〜
        }
        androidTestFlavor2 {
            setRoot('androidTestFlavor2')
            〜〜〜
        }
    }
    productFlavors {
        Flavor1 {
            〜〜〜
        }
        Flavor2 {
            〜〜〜
        }
    }
    dependencies {
        androidTestFlavor1Compile 〜〜〜
        androidTestFlavor2Compile 〜〜〜
    }
}

あとは通常の androidTest と同じように、sourceSetsなどを設定します。

テストする際には、 Build Variant からフレーバーを選択します(例:Flavor1Debug)。テストの Configrations (メニューバーからプルダウンで選択する)は、テストするアプリが同じなので使いまわせます。

補足

  • この記事は Android Studio 2.0 で検証しています
  • androidTestを設定した時は、全フレーバー共通のテストとなる
  • フレーバー名の頭文字を小文字にしていると、テストが認識されない場合がある
  • 適切に設定すれば JUnit4 と Espresso 等のテスト共存も可能だと思われる(未検証)
  • Eclipse のテストを持ってくる場合、 productIDAndroidManifest.xml の設定に注意

参考

3
3
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
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?