Running gradle's connectedAndroidTest on a specific device

后端 未结 5 1401
时光取名叫无心
时光取名叫无心 2020-12-25 13:24

How do you run connectedAndroidTest on a particular device?

I would expect something like:

./gradlew connectedAndroidTest -DconnectedAnd         


        
5条回答
  •  梦谈多话
    2020-12-25 13:43

    I created an "hack" to be able to do it.. put this block in the android section of your build.gradle, and then you have to set the ANDROID_HOME env variable to the sdk folder, and the UNIT_TESTS_DEVICE_ID env variable with the serial number of the device on which you want to run tests on.

    deviceProvider(new com.android.builder.testing.ConnectedDeviceProvider(file(System.getenv("ANDROID_HOME") + File.separator + "platform-tools" + File.separator + "adb")) {
        public String getName() {
            return "singleDevice"
        }
    
        public List getDevices() {
            List devices = super.devices;
            List toReturn = new ArrayList<>();
            String deviceSerialNum = System.getenv("UNIT_TESTS_DEVICE_ID");
            devices.each {
                if (it.getSerialNumber().equals(deviceSerialNum)) toReturn.add(it);
            }
            if (toReturn.isEmpty()) {
                throw new RuntimeException("Device for unit tests not found!");
            }
            return toReturn;
        }
    })
    

    Then you use the task singleDeviceAndroidTest{Variant} to run the tests. Tested only on gradle plugin version 1.0.0.

提交回复
热议问题