Android JUnit4 runner не запускает тесты, не начинающиеся с 'test'

Существует ActivityInstrumentationTestCase2, у которого есть бегунок AndrodJUnit4. Есть методы тестирования, аннотированные @Test. однако кажется, что методы, которые не начинаются с префикса 'test', вообще не выполняются командой gradle (в Android Studio такой проблемы нет).

@RunWith(AndroidJUnit4.class)
public class MyActivityTest extends ActivityInstrumentationTestCase2<MyActivity> {


  public MyActivityTest() {
    super(MyActivity.class);
  }

  @Before
  public void setup() throws Exception {
    super.setUp();
    injectInstrumentation(InstrumentationRegistry.getInstrumentation());

    getActivity();
  }

  @Test
  public void testOne() {
    // this runs from gradle
  }

  @Test
  public void two() {
    // this does not run from gradle. However runds from Android Studio
  }

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

Фрагмент из build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion '21.1.2'

    defaultConfig {
        ....
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
}

dependencies {

    compile 'com.android.support:support-annotations:22.2.0'
    ...

    testCompile 'junit:junit:4.12'
    testCompile 'org.mockito:mockito-all:1.10.19'

    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2')
    androidTestCompile('com.android.support.test.espresso:espresso-intents:2.2')

    androidTestCompile('com.android.support.test:runner:0.3')
}

1 ответ

Мне кажется, что вы используете JUnit 3, а не JUnit 4, вы не выходите из InstrumentTestCase2 вместо этого вы используете @Rule аннотаций.

@RunWith(AndroidJUnit4.class)
@LargeTest
public class ApplicationTest {

    @Rule
    public ActivityTestRule<MyActivity> mActivityRule = 
        new ActivityTestRule<>(MyActivity.class);

    @Test
    public void listGoesOverTheFold() {
        onView(withText("Hello world!")).check(matches(isDisplayed()));
    }

    @Test
    public void assertFail(){
        assertFalse(false);

    }

}
Другие вопросы по тегам