It is possible to share code between this two test modes in Android Studio? I have a set of Mock Utils class's that I need to access in both of the test modes.
-
There is an open discussion about supporting test fixtures in Android Studio, see https://issuetracker.google.com/issues/139438142 – Akhha8 Jun 18 '22 at 00:57
5 Answers
Finally I found the solution (workaround) thanks to a blog post from Dan Lew (http://blog.danlew.net/2015/11/02/sharing-code-between-unit-tests-and-instrumentation-tests-on-android/).
The solution I've come up with is to leverage source sets to define common code. First, I put my shared test code into src/sharedTest/java1 .
android {
sourceSets {
String sharedTestDir = 'src/sharedTest/java'
test {
java.srcDir sharedTestDir
}
androidTest {
java.srcDir sharedTestDir
}
}
}
What it's doing above is adding my shared code directory to both the test and androidTest source sets. Now, in addition to their default Java sources, they'll also include the shared code.
Edit (20022-08-15): This no longer works, for a discussion of why, and the (now) recommneded way to achieve sharing of code, see this question and the accepted answer: Shared srcDirs between test and androidTest, unresolved references after upgrade to Android Studio Chipmunk (IntelliJ 2021.2.1)

- 350
- 4
- 13

- 9,921
- 4
- 36
- 57
-
1
-
8
-
@StefanTo I have proposed an edit that links to this Question (and Accepted Answer) that explains why, and has Google's recommendation, along with some gotchas: https://stackoverflow.com/questions/72218645/shared-srcdirs-between-test-and-androidtest-unresolved-references-after-upgrade – IainCunningham Aug 15 '22 at 15:54
Sharing code via common folder and sourceSets no longer works as of Android Studio chipmunk. BUT, there is another method to make it work:
Basically, create an android library (sharedTestCode), depend on it in your app via testImplementation and androidTestImplementation. In the sharedTestCode build.gradle file, depend on the app. You should now be able to create shared test data and reference them in both types of tests.
Here is a sample project with this setup working:
https://drive.google.com/file/d/1I2CZhTxHGRgCN9UCEjIuWUfFnGxTF_Cv/view?usp=sharing

- 5,350
- 4
- 28
- 38
-
-
It's important to know that things such as flavors need to match or this will not work. See this SO Question and accepted Answer for full discussion and recommendation from Google: https://stackoverflow.com/questions/72218645/shared-srcdirs-between-test-and-androidtest-unresolved-references-after-upgrade – IainCunningham Aug 15 '22 at 15:53
For multiple modules project
sourceSets {
test.java.srcDirs += ["${project(':module-name').projectDir}/src/sharedTest/java",
"src/test/java"]
}

- 2,258
- 1
- 21
- 26
The accepted answer does not seem to work with Android Studio Chipmunk anymore...
Here is one (not ideal) workaround:
android {
sourceSets.main.java.srcDirs += "$projectDir/src/testShared/java"
}
-
2Note that with this approach your test classes will be included with your production code when you build. – Veselin Todorov May 23 '22 at 14:16
-
-
1
Try assigning the sourceSets manually:
sourceSets {
val sharedTestDir = file("src/sharedTest/java")
getByName("test").java.srcDirs(sharedTestDir)
getByName("androidTest").java.srcDirs(sharedTestDir)
}

- 12,644
- 7
- 58
- 82