Dmitriy Voronin
Dmitriy Voronin

Reputation: 648

A way to share code between multiple android test modules

I want to achieve something like this:

[ComponentTestsModule] com.android.test
[FunctionalTestsModule] com.android.test
both depends on
 -> [TestLibraryModule] ?
which depends on
   -> [AppModule] com.android.application

Is there any way to do it with android Gradle plugin 3.0+?

Why I need multiple test modules?

I want different test runners for different types of tests, also target different variants. It is working right now with single codebase under androidTest, but with ugly switches in the custom test runner.

Why I need a test library module?

I want to share the same page-objects between different types of tests, and maybe some utility code. Problem is: that page objects must have access to R class of app (locators: R.id.*)

None of the module types I'm aware of can depend on APK-producing module, expect from com.android.test, but i cannot depend from com.android.test with another com.android.test.

Upvotes: 27

Views: 3419

Answers (2)

ThaiPD
ThaiPD

Reputation: 3741

I have recently faced this problem and just want to share my solution in case some body needs it.

What I have done so far (not a perfect solution - I guess, but at least it works).

  1. Create a new module named testing_base or some thing like that

  2. In testing_base module only put things related to testing (Like the code you want it to be shared between modules) in to normal packages, NOT in the test packages/folders.

  3. From others module, try to import things from testing_base module

ex: testImplementation project(":testing_base")

Hope it can helps someone, happy testing!

Upvotes: 8

Code-Apprentice
Code-Apprentice

Reputation: 83517

I want different test runners for different types of tests

To run a test class with a specific test runner, use the @RunWith annotation.

also target different variants

To target a specific variant, create your tests in app/src/androidTestVariantName for instrumented tests or in app/src/testVariantName for unit tests.

Upvotes: -4

Related Questions