atok
atok

Reputation: 5938

Share code between Local Tests and Instrumented Test?

Is there a way to share code between those two without creating another module/external library ?

Upvotes: 1

Views: 172

Answers (2)

Kylaaa
Kylaaa

Reputation: 7188

As of around June 2022, Android Studio Bumblebee stopped supporting the existing answer. (The reason can be found in this developer thread) Now, the appropriate solution is to create a new library module, move your shared code into that module, and update your app/build.gradle file to depend on the library module for its tests and androidTests.

So follow these steps :

1. Add a new module.

Go to File > Project Structure > Modules. Hit the plus button to add a new module. Create a Kotlin library named "testShared" (or whatever you want your shared code package to be).

An image of the Project Structure dialog.

2. Set up the build dependencies

You can do this step in the Project Structure dialog under the Dependencies section, but if you want to do it manually, follow these steps.

Open your app/build.gradle file, and scroll down to the dependencies section. Add the newly created module as a dependency for your tests and instrumented tests.

dependencies {
    ... all of your regular dependencies...


    testImplementation project(":testShared")
    androidTestImplementation project(":testShared")
}

Next, if you want to be able to reference your app code in the shared test helper code, you'll need to add a dependency onto the app. In the testHelpers/build.gradle file, add the following into the dependencies section.

dependencies {
    implementation project(path: ':app')
}

3. Move all of your shared code into this testShared library

Don't forget to update the package references in your shared test files and your old tests to reflect the new paths.

And that should be it!

4. Miscellaneous stuff to get it to work...

The above steps are mostly what you need to get it working, but I ran into some other blockers where the library itself refused to build. If you're running into issues, I'll use this section as an FAQ for hotfixes.
  1. Update the testShared/build.gradle file to be an android library.
plugins {
    id 'com.android.library'
    id 'kotlin-android'
}

android {
    compileSdk 31
}

dependencies {
    // ADD ANY NECESSARY DEPENDENCIES HERE
    implementation 'org.jetbrains.kotlin:kotlin-ksp:1.4.0-rc-dev-experimental-20200828'

    implementation project(path: ':app')
}
  1. Create a relatively empty AndroidManifest.xml file in testShared/main
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.YOURWEBSITE.testshared">
</manifest>
  1. Update AndroidStudio to be at least version Chipmunk | 2021.2.1 Patch 2

  2. Update the Gradle plugin to be at least version 7.2.2.

Upvotes: 0

nAndroid
nAndroid

Reputation: 960

It's been 5 years so hopefully you figured it out, but in case someone stumbles on this.

 // This allows us to share classes between androidTest & test directories.
android.sourceSets {
    test {
        java.srcDirs += "$projectDir/src/testShared"
    }
    androidTest {
        java.srcDirs += "$projectDir/src/testShared"
    }
}

Upvotes: 1

Related Questions