I have a library module that is used by two android applications and I want to add espresso tests to the Library module so that both apps can run common set of tests. Is there an example available where espresso tests are added in library module?
2 Answers
The setup is very similar to adding Espresso tests to a library module in an application module.
Here is an example with library tests and an app that depends on that library.

- 3,035
- 18
- 24
The test runner will collect the functions annotated with @Test so you will have to duplicate those in your modules. However, you can still just call a reused function that is defined in a separate test module.
Setup your modules like this:
library -> libraryTest -> app(1/2)
Your libraryTest build.gradle would contain this:
implementation project(path: ':library')
Just make sure you maintain your code in your test module (libraryTest) under the main sources libraryTest/src/main/java.
Your app(s) build.gradle would contain this:
implementation project(path: ':library')
androidTestImplementation project(path: ':libraryTest') // for instrumentation tests
// testImplementation project(path: ':libraryTest') // for unit tests
You could then have a test function in your libraryTest module:
object RealSomethingTests {
fun realTest1(){
// test code
}
}
And tests in your app modules would just call this function.
app1 module:
class App1SomethingTests {
@Test
fun realTest1() = RealSomethingTests.realTest1()
}
app2 module:
class App2SomethingTests {
@Test
fun realTest1() = RealSomethingTests.realTest1()
}
If your tests need to know the app modules (e.g. they need access to layout ids or similar) they do not belong in a shared module but into the app modules themselves. This setup only really makes sense when the tests only need to know the library module.

- 2,712
- 2
- 20
- 32