androidgradledynamic-feature-module

How to execute UI tests for a Dynamic Feature Module via Android Studio?


I have a regular :app module and a DFM :feature.

I place the tests for the DFM in androidTest source set inside :feature.

When I try to launch the test using run configuration created by Android Studio it silently fails with Test framework quit unexpectedly.

If I launch the test through the terminal with the command displayed by Android Studio in Run tab I will see:

$ adb shell am instrument -w -m    -e debug false -e class 'com.amazing.feature.AmazingTest' com.amazing.application.test/com.amazing.feature.TestRunner
...
Unable to find instrumentation info for: ComponentInfo{com.amazing.application.test/com.amazing.feature.TestRunner}

Which is not surprising, since the path to the runner is different:

$ adb shell pm list instrumentation
instrumentation:com.amazing.feature.test/com.amazing.feature.TestRunner (target=com.amazing.application)

The test works if I use that instrumentation on the commandline.

Is there a way to teach Android Studio do that?


Solution

  • I had the same issue. Turns out gradle creates a wrong targetPackage in the <instrumentation> tag in the dynamic feature module's AndroidManifest.xml of the androidTest APK. You can fix this by setting the testApplicationId in the module's build.gradle defaultConfig.

    defaultConfig {    
        testApplicationId 'com.amazing.feature.test' // Set the test package here!
        testInstrumentationRunner 'com.amazing.feature.TestRunner'
        [...]
    }
    

    More documentation on this on developer.android.com.