Feedbacker
Feedbacker

Reputation: 959

Place instrumentation tests in feature modules

I am having a multi-module architecture with an app module and feature/library modules. Currently, there are some feature instrumentation tests in the app module. I would like to move instrumentation tests that test specific features in those respective feature modules under the androidTest folder. After some trial and error this is the state:

  1. In app module gradle's file add
        androidTest {

            java.srcDirs += [
                    "${project(':feature').projectDir}/src/androidTest/kotlin"
            ]
            assets.srcDirs = [
                    "${project(':feature').projectDir}/src/androidTest/assets/",
            ]
        }
  1. Created a "base-test" module where the base test classes are (BaseActivity and BaseUiTest)

  2. In app and feature gradle file add

    defaultConfig {
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
  1. In app module's androidTest AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.xxx">

    <application
        android:theme="@android:style/Theme.Light"
        android:name=".App"
        android:label="Test app">
    </application>

</manifest>

When I run the test from the android studio interface, the test is successful. However, when running the test from the command line (./gradlew :feature:connectedAndroidTest) the test fails due to app crashing at runtime (due to koin modules not being initialized - which they are at CustomApplication's onCreate)!

Even more weirdly, if I move the feature module's androidTest folder in its own module (e.g. feature-test module which depends on feature module) it works!

Is there any standardized way to put instrumented tests in each feature module in a multi-module setup? Why does from android studio run the test successfully but not from the command line?

Upvotes: 3

Views: 1145

Answers (1)

Mohamed Khaled
Mohamed Khaled

Reputation: 157

with Gradle:

./gradlew :app:connectedDebugAndroidTest -Pandroid.testInstrumentationRunnerArguments.class=com.example.feature.ExampleActivityTest

with adb:

adb shell am instrument -w -r -e debug false -e class com.yourapp.feature.ExampleActivityTest yourApplicationId.test/androidx.test.runner.AndroidJUnitRunner

More info: https://developer.android.com/studio/test/command-line#run-tests-with-adb

Upvotes: 0

Related Questions