Running a specific instrumentation unit test with Gradle

后端 未结 8 943
既然无缘
既然无缘 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 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 extends ActivityInstrumentationTestCase2 {
        //...
        @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 extends ActivityInstrumentationTestCase2 {
        //...
        @Override
        protected void runTest() throws Throwable {
            if("MyTestClassName".equals(getClass().getSimpleName()) 
               && "testMethodName".equals(getName())) {
                super.runTest();
            }
        }
        //...
    }
    

提交回复
热议问题