7
7

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でAndroidJUnit4を使うとテストが実行されない場合がある

Posted at

問題

ActivityInstrumentationTestCase2を継承したクラスでAndroidJUnit4を使うとテストが実行されないことがあります
とりあえず、Android Studio 1.1.0 + Espresso 2.0で確認

MainActivityTest.java
@RunWith(AndroidJUnit4.class)
@LargeTest
public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {

    private MainActivity mActivity;

    public MainActivityTest() {
        super(MainActivity.class);
    }

    @Before
    public void before() throws Exception {
        this.setUp();
        injectInstrumentation(InstrumentationRegistry.getInstrumentation());
        mActivity = getActivity();
    }

    @After
    public void after() throws Exception {
        this.tearDown();
    }

    @Test
    public void checkClick() {
        //ボタンをクリック
        onView(withId(R.id.button)).perform(click());
    }
}

アノテーションをつけたのにも関わらず、checkClickは実行されません

現状の回避方法

MainActivityTest.java
@RunWith(AndroidJUnit4.class)
@LargeTest
public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {

    //〜中略〜
    @Test
    public void test() {}

    @Test
    public void checkClick() {
        //ボタンをクリック
        onView(withId(R.id.button)).perform(click());
    }
}

このようにtestを含んだメソッドを1個余計に作るだけでOKです
これでtestとcheckClickが実行されます

普通のクラスやApplicationTestCaseだと問題ないんですけど、何が原因なんでしょうね?

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?