如何从终端运行仪器@SmallTests?

时间:2017-01-03 15:30:25

标签: java android unit-testing junit instrumentation

我无法从终端运行我的@SmallTests。我已经创建了我的设置示例,我希望您指出我做错了什么。

  1. 我正在使用我自己的默认Runner类,如下所示:

    public class MyAppTestRunner extends AndroidJUnitRunner {
    
        @Override
        public Application newApplication(ClassLoader cl, String className, Context context) 
                throws InstantiationException, IllegalAccessException, ClassNotFoundException {
            return super.newApplication(cl, MyAppTestApplication.class.getName(), context);
        }
    }
    
  2. 我已经在我的gradle中设置了这个跑步者:

    android {
        defaultConfig {
            testInstrumentationRunner "com.MyApp.instrumentation.config.MyAppTestRunner"
        }
        testBuildType 'integrationTest'
    }
    
  3. 我也在使用这些依赖项:

    androidTestCompile com.android.support.test:runner:0.5
    androidTestCompile com.android.support.test:rules:0.5
    
  4. 然后我创建了一个简单的类,其中一个测试用@SmallTest注释:

    import android.content.Intent;
    import android.support.test.filters.SmallTest;
    import android.support.test.rule.ActivityTestRule;
    import android.support.test.runner.AndroidJUnit4;
    
    import com.MyApp.a.ui.activity.WelcomeActivity;
    
    import org.junit.Before;
    import org.junit.Rule;
    import org.junit.runner.RunWith;
    
    
    @RunWith(AndroidJUnit4.class)
    public class ExampleTestPackage {
    
        @Rule
        public ActivityTestRule<WelcomeActivity> activityRule = new ActivityTestRule<>(
            WelcomeActivity.class, true, false
        );
    
        @Before
        public void before() {
            activityRule.launchActivity(new Intent());
        }
    
        @SmallTest
        public void exampleTest() {
            // some test code
        }
    }
    
  5. 所以这是我的设置。根据我使用的documentation of AndroidJUnitRunner,使用小注释运行我的测试我应该做这样的事情:

      

    运行特定的测试大小,即使用SmallTest或   MediumTest或LargeTest:adb shell am instrument -w -e size   [小|介质|大]   com.android.foo/android.support.test.runner.AndroidJUnitRunner

    所以我在终端做的是:

    adb shell am instrument -w -e size small com.MyApp.debug1.test/com.MyApp.instrumentation.config.MyAppTestRunner
    

    但是找不到我的@SmallTest,输出是:

      

    时间:0.001

         

    确定(0测试)

    此外,如果我从Android Studio启动测试类,那么我可以看到此错误:

    java.lang.Exception: No runnable methods
    at org.junit.runners.BlockJUnit4ClassRunner.validateInstanceMethods(BlockJUnit4ClassRunner.java:191)
    at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:128)
    at org.junit.runners.ParentRunner.validate(ParentRunner.java:416)
    at org.junit.runners.ParentRunner.<init>(ParentRunner.java:84)
    at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:65)
    at android.support.test.internal.runner.junit4.AndroidJUnit4ClassRunner.<init>(AndroidJUnit4ClassRunner.java:37)
    at android.support.test.runner.AndroidJUnit4.<init>(AndroidJUnit4.java:36)
    at java.lang.reflect.Constructor.newInstance(Native Method)
    at android.support.test.internal.runner.junit4.AndroidAnnotatedBuilder.buildAndroidRunner(AndroidAnnotatedBuilder.java:83)
    at android.support.test.internal.runner.junit4.AndroidAnnotatedBuilder.runnerForClass(AndroidAnnotatedBuilder.java:62)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
    at org.junit.runner.Computer.getRunner(Computer.java:40)
    at org.junit.runner.Computer$1.runnerForClass(Computer.java:31)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:101)
    at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:87)
    at org.junit.runners.Suite.<init>(Suite.java:81)
    at org.junit.runner.Computer.getSuite(Computer.java:28)
    at android.support.test.internal.runner.TestRequestBuilder.classes(TestRequestBuilder.java:789)
    at android.support.test.internal.runner.TestRequestBuilder.build(TestRequestBuilder.java:753)
    at android.support.test.runner.AndroidJUnitRunner.buildRequest(AndroidJUnitRunner.java:354)
    at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:260)
    at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1879)
    

    如果我将@SmallTest更改为@Test(import org.junit.Test;)

    ,当然可以修复

    我在那里使用错误的课吗?

1 个答案:

答案 0 :(得分:0)

来自@SmallTest的注释@MediumTestandroid.support.test.filters等不会替换@Test注释。需要@Test注释来通知JUnit相应的方法是测试用例(http://junit.org/junit4/javadoc/latest/org/junit/Test.html)。

在您的情况下,这将有效:

@SmallTest
@Test
public void exampleTest() {
    // some test code
}