Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android multi module test dependency

With Android Studio 3.0 / android_gradle_version = '3.0.1' / gradle-4.5

Let's say I have two android modules

module-base
module-a

When I want to access sources from module-base in module-a , I just need to write this in my module-a.gradle

dependencies {
implementation project(path: ':module-base')
}

But, what if I want to access test sources from module-base in test of module-a? Here does not work approach like above

dependencies {
testImplementation project(path: ':module-base')
}

I found lot of advices (few years old) which says something like

    compileTestJava.dependsOn tasks.getByPath(':module-base:testClasses')
    testCompile files(project(':module-base').sourceSets.test.output.classesDir)

or testCompile project(':module-base).sourceSets.test.classes

But no one from mentioned works. There is always something wrong from the compiler point of view :-/

Can you someone help me how to create Android test code dependency between two modules?

like image 723
kotoMJ Avatar asked Mar 22 '18 19:03

kotoMJ


People also ask

What is multi module in Android?

A project with multiple Gradle modules is known as a multi-module project.

When should you use the androidTest directory to store your test classes?

When should you use the androidTest directory to store your test classes? when the tests consist only of unit tests. when the number of tests to run is large(500+).

How do I run a Gradle test from the command line?

Use the command ./gradlew test to run all tests.

What are instrumented tests?

Note: Instrumented test, also known as instrumentation tests, are initialized in a special environment that gives them access to an instance of Instrumentation. This class provides access to the application context and APIs to manipulate the app under test and gives instrumented tests their name.


2 Answers

Actually I find just workaround for this. Don't use test sources from module-base but use sources from test related module module-testutils which is defined like this

dependencies{
   testImplementation project(':module-testutils')
}

Thus you can share common test code which will be excluded for non-testable apk.

like image 60
kotoMJ Avatar answered Oct 20 '22 14:10

kotoMJ


In case anyone else ends up here, one way to accomplish this is to define the target module as a source set. Let's assume test-mdoule is the module we want to import stuff from, we can do it this way:

android {
    sourceSets {
        // non-instrumental test example
        test {
            java.srcDir project(":test-module").file("src/test/java")
        }
        // instrumental test example
        androidTest {
            java.srcDir project(":test-module").file("src/androidTest/java")
        }
    }
}

Reference: https://jonnycaley.medium.com/sharing-test-code-in-a-multi-module-android-project-7ce4de788016

like image 40
william xyz Avatar answered Oct 20 '22 15:10

william xyz