Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use code in androidTest directory of another module

I add two modules in my AndroidStudio project:

app-base
|
|----src
      |____androidTest
                |________MyTestBase.java          

app
|
|----src
      |____androidTest
                |________MyTest.java

Some common test class are defined in app-base's androidTest, and are used in app'androidTest.

I have tried to add the following code in app's build.gradle:

    evaluationDependsOn(':app-base')

    compile project(':app-base')

    androidTestCompile project(':app-base')

I have include both app-base and app in settings.gradle, the output of gradlew projects is:

Root project 'MyProject'
+--- Project ':app-base' 
+--- Project ':app'

No compile error by this way, but when I Run MyTest in ide, it said class MyTestBase is not found.

Do you know what's wrong? Any ideas are appreciated. Thanks.

like image 250
maoruilin Avatar asked Dec 06 '14 14:12

maoruilin


3 Answers

I've been looking for a solution to this for a while and finally found something that works for me.

This will allow you to put common testing classes in another module.

android {
    ...

    sourceSets {
        androidTest.java.srcDirs += ["${project('match the module name that is currently in the dependencies').projectDir}/src/androidTest/java"]
    }
}

So for the example above it would probably look something like

androidTest.java.srcDirs += ["${project(':appbase').projectDir}/src/androidTest/java"]
like image 76
Stimsoni Avatar answered Nov 09 '22 15:11

Stimsoni


I suggest you the following strategy : create a new module test-utils and put MyTestBase.java in the main sources of this module.

test-utils
|
|----src
      |____main
             |________MyTestBase.java

Then you add this test-utils as a test dependency in all modules where it is required

androidTestCompile project(':test-utils')
like image 14
ben75 Avatar answered Nov 09 '22 17:11

ben75


With little change in Stimsoni's answer, this is finally worked for me. I added this to my app gradle:

sourceSets {
    test.java.srcDirs += ["${project(':app-base').projectDir}/src/test/java"]
}
like image 3
Besat Avatar answered Nov 09 '22 17:11

Besat