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

Viewed 329

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?

2 Answers

I got this issue as well, just after upgrading to AGP 7 and the latest Android Studio. In my case it was a bit different, I have the same repository checked out twice, so work on 2 branches simultaneously. In my case, it happened that one branch + android studio instance would work, it did select the correct TestRunner, and the other instance did not work. This lead me to the simple solution of:

  1. Build -> Clean Project
  2. Remove any remaining build folders (was just 1 for me)

Then retry and it did select the correct TestRunner. Other things ive tried, of which I do not know whether or not they helped, are the following:

  • Make sure I use Java 11 (since i'm on AGP 7)
    • Project Structure -> Gradle settings -> Gradle JDK should be something like 11
  • Disable the experimental feature Do not build Gradle task list during Gradle sync (automatically enabled on Android Studio Arctic Fox)

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.

Related