23

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.

Sandro Machado
  • 9,921
  • 4
  • 36
  • 57
  • 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 Answers5

27

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)

IainCunningham
  • 350
  • 4
  • 13
Sandro Machado
  • 9,921
  • 4
  • 36
  • 57
  • 1
    What's about doing this across the modules? – X-HuMan Jan 15 '17 at 14:13
  • 8
    It seems this does not work for Android Studio Chipmunk anymore. – StefanTo May 17 '22 at 08:22
  • @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
6

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

Sean Blahovici
  • 5,350
  • 4
  • 28
  • 38
  • this doesn't work with internal classes :( – Sofi Software LLC May 31 '22 at 22:13
  • 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
3

For multiple modules project

sourceSets {
        test.java.srcDirs += ["${project(':module-name').projectDir}/src/sharedTest/java",
                              "src/test/java"]
    }
takharsh
  • 2,258
  • 1
  • 21
  • 26
1

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"
}
Bill Mote
  • 12,644
  • 7
  • 58
  • 82
StefanTo
  • 971
  • 1
  • 10
  • 28
0

Try assigning the sourceSets manually:

sourceSets {
  val sharedTestDir = file("src/sharedTest/java")
  getByName("test").java.srcDirs(sharedTestDir)
  getByName("androidTest").java.srcDirs(sharedTestDir)
}
Bill Mote
  • 12,644
  • 7
  • 58
  • 82