Android JUnit4运行器不会运行不以' test'开头的测试

时间:2015-07-03 08:14:46

标签: android android-gradle android-espresso

有一个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 个答案:

答案 0 :(得分: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);

    }

}