JUnit で特定のテストケース(クラスや関数)を一時的に無効にしておきたい場合は、次のようなアノテーションをクラスや関数に付けます。
- JUnit4 の場合: @Ignore アノテーション (org.junit.Ignore)
- JUnit5 の場合: @Disabled アノテーション (org.junit.jupiter.api.Disabled)
// import org.junit.Ignore;
// import org.junit.Test;
public class MyClassTest {
@Test @Ignore public void testSomething() {
// ...
}
}
// import org.junit.Ignore
// import org.junit.Test
class MyClassTest {
@Test @Ignore fun testSomething() {
// ...
}
}
@Ignore
(@Disabled
) に文字列パラメータを渡すと、なぜそのテストを無効にしているのかを示すことができます。
@Ignore("HogeHogeのパラメータを整理中")
@Test
fun testSomething() {
// ...
}
このメッセージは JUnit でテストを実行したときに表示されます。 下記は、Android Studio 上で JUnit によるテストを実行したときの表示例です。 16 個のテスト関数のうち 1 つが無視され、その理由が表示されています。
