Running a specific instrumentation unit test with Gradle

后端 未结 8 782
既然无缘
既然无缘 2020-11-30 04:27

Is there a way to run a specific Android instrumentation unit test using Gradle? I\'ve tried

gradle -Dtest.single=UnitTestName connectedInstrumentTest


        
相关标签:
8条回答
  • 2020-11-30 04:59

    the Gradle command does not work for me. I used below mentioened adb command. for this you need to build your apk first.

    adb shell am instrument -w -r -e package -e debug false .debug.test/android.support.test.runner.AndroidJUnitRunner

    0 讨论(0)
  • 2020-11-30 05:03

    This works if you're using an instrumentationTestRunner:

    ./gradlew test -Pandroid.testInstrumentationRunnerArguments.class=<pkg>.YourClassName
    

    Using gradle 2.10 and android gradle plugin 2.0.0-beta2.

    Since you know what test(s) you want to run, you probably know which module / flavor to use too. You can help Gradle out by specifying the exact module and Gradle task. So if your test is in the app module and you want to test the debug flavor:

    ./gradlew app:connectedDebugAndroidTest -Pandroid.testInstrumentationRunnerArguments.class=<pkg>.YourClassName
    

    You can get even more fancy with the tests_regex argument instead:

    ./gradlew app:connectedDebugAndroidTest -Pandroid.testInstrumentationRunnerArguments.tests_regex=PartialClassName*
    ./gradlew app:connectedDebugAndroidTest -Pandroid.testInstrumentationRunnerArguments.tests_regex=partialMethodName*
    
    0 讨论(0)
  • 2020-11-30 05:06

    The pattern is -D<testTaskName>.single=<TestClass> so in your example it should be:

    gradle -DconnectedInstrumentTest.single=UnitTestName connectedInstrumentTest
    

    NOTE: This answer is outdated. You should use the --tests switch in the latest versions of Gradle. (see other answers for an explanation)

    0 讨论(0)
  • 2020-11-30 05:08

    Erdi's answer didn't work for me but I have a single parent for all my test classes so I was able to do this:

    public abstract class BaseEspressoTest<T extends Activity> extends ActivityInstrumentationTestCase2<T> {
        //...
        @Override
        protected void runTest() throws Throwable {
            if(getClass().getSimpleName().equals("MyTestClassName")) {
                super.runTest();
            }
        }
        //...
    }
    

    This executes only MyTestClassName. We can extend it further to execute only specific test method (or methods):

    public abstract class BaseEspressoTest<T extends Activity> extends ActivityInstrumentationTestCase2<T> {
        //...
        @Override
        protected void runTest() throws Throwable {
            if("MyTestClassName".equals(getClass().getSimpleName()) 
               && "testMethodName".equals(getName())) {
                super.runTest();
            }
        }
        //...
    }
    
    0 讨论(0)
  • 2020-11-30 05:12

    You should not forget to specify a build variant name after test property declaration like

    -Dtest<buildVariantName>=<yourTestName>.

    Like if you have a debug build type which gives you debug variant after compilation, then if you want to run a test only for this build variant you should declare a command like this:

    ./gradlew -DtestDebug=UnitTestName testDebug

    0 讨论(0)
  • 2020-11-30 05:18

    Using test.single appears to be deprecated. The new correct way to do this is

    ./gradlew :<module>:test --tests <pattern>
    

    where <pattern> could be something like:

    • com.example.MyTest to run all test methods in com.example.MyTest
    • *MyTest to match every method in every class whose name ends with MyTest
    • *.MyTest.myMethod to run a specific test method in class MyTest in any package

    If you have a multi-project build, make sure to give the module path before the test task; otherwise you'll get a misleading error message when it searches for your test pattern in every subproject.

    None of this is documented on the Gradle site anywhere I could find it.

    0 讨论(0)
提交回复
热议问题