Running gradle's connectedAndroidTest on a specific device

后端 未结 5 1392
时光取名叫无心
时光取名叫无心 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:37

    It's not supported. The documentation for connectedCheck at http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Android-tasks, which delegates to connectedAndroidTest for these sorts of on-device non-UI-automated tests, explicitly states:

    Runs checks that requires a connected device or emulator. They will run on all connected devices in parallel.

    There is a feature request for the ability to select individual devices; you can track its progress at https://code.google.com/p/android/issues/detail?id=66129

    0 讨论(0)
  • 2020-12-25 13:43

    Use the ANDROID_SERIAL variable

    You can do this two ways:

    1. Set environment variable

    # Set once; all following gradlew commands will use this
    export ANDROID_SERIAL=1000AB0123456YZ
    
    ./gradlew <...>
    

    2. "Set" for just a command

    ANDROID_SERIAL=1000AB0123456YZ ./gradlew <...>
    

    If you set/exported ANDROID_SERIAL (method #1), you can use this to override that for a single command.

    Note

    This works with emulator identifiers (e.g., "emulator-5554"), too.

    0 讨论(0)
  • 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<? extends com.android.builder.testing.api.DeviceConnector> getDevices() {
            List<com.android.builder.testing.api.DeviceConnector> devices = super.devices;
            List<com.android.builder.testing.api.DeviceConnector> 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.

    0 讨论(0)
  • 2020-12-25 13:49

    @MotohawkSF's answer did not worked for me in Android Studio terminal. What worked though was using the name of the device after running adb devices

    > adb devices
    List of devices attached
    192.168.22.122:5555    device
    emulator-5554   device
    

    then

    set ANDROID_SERIAL=192.168.22.122:5555
    gradlew someTask
    
    0 讨论(0)
  • 2020-12-25 13:53

    It should be possible now. Just set ANDROID_SERIAL environment variable to the device id you want your tests to run on.

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