LoginSignup
48
27

More than 5 years have passed since last update.

GradleでJUnit Testをフィルタ実行する方法

Posted at

--testsオプション

以下のようにして対象のテストクラス(HogeTest)を指定できる

./gradlew test --tests com.example.HogeTest

メソッドまで指定することも可能

./gradlew test --tests com.example.HogeTest.fooMethodTest

ワイルドカード(*)を利用して複数のテストを指定することもできる

./gradlew test --tests com.example.\*Test

このオプションは「実行するテスト」は指定できるが、「実行しないテスト」を指定することはできない。
-Dtest.single というオプションもあるが、--testsで代替可能。

include/exclude関数

テスト対象に包含/除外するテストを指定できる

test {
    include 'com/example/HogeTest.class'
    exclude '**/*Foo*'
}

--testsとは異なり、.classファイルへのパスを指定する。

Test Grouping

build.gradleに記述することで、特定のカテゴリに属するテストを包含/除外することができる

test {
    useJUnit {
        includeCategories 'com.example.CategoryA'
        excludeCategories 'com.example.CategoryB', 'com.example.CategoryC'
    }
}

カテゴリはInterfaceとして作成し、@Categoryアノテーションを通じて指定する

interface MyCategory

@Category(MyCategory::class)
class HogeTest {
    // some tests...
}

ちなみにこれを使うと、@RunWith(JUnitRunner::class)を指定したテストが認識されなくなった(謎)。

おまけ:Androidでの設定

Androidではbuild.gradleの以下のブロックに設定を記述する

android {
    unitTests.all {
        // here
    }
}

参考

48
27
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
48
27