Android Test Fixtures in AGP 8.5+: A Quick Guide

Jansel Valentin
2 min readJul 9, 2024

--

Android Test Fixtures

The long-awaited test fixture support in the Android Gradle Plugin has finally arrived in the most recent AGP 8.5 release.

Test fixtures have been available for Gradle JVM projects for a while. Still, they were incompatible and unsupported by the Android Gradle Plugin since the required Gradle outgoing configuration failed to be published properly in Android projects.

Wait no more!!

Android test fixtures require AGP 8.5.0 and Kotlin 1.9.20+

With that said, let’s explore how you can expose test fixtures from a library module to any other one interested in consuming the shared test logic.

1. Opt-in to the feature

The feature is still experimental, hence it requires explicit opt-in.

gradle.properties

android.experimental.enableTestFixturesKotlinSupport=true

2. Expose library test fixtures

Enable test fixture in the library module.

build.gradle.kts

android {
testFixtures {
enable = true
}
}

If you are using the Android new variant API, the following could do as well

androidComponents {
finalizeDsl {
it.testFixtures {
enable = true
}
}
}

This is enough for it to expose the test fixtures to consumers. Enabling this option enabled as well a new testFixture source set in the library module. This source set is where the shared test code should reside.

Let’s suppose your library exposes an interface such as

data class Customer(val id: String)

interface CustomerService {
fun getCustomer(id: String): Customer
}

and that you want to expose a fake for this service as a fixture to downstream test consumers, it suffices to place it under the testFixtures source set such as

class FakeCustomerService : CustomerService {
override fun getCustomer(id: String): Customer = Customer(id)
}

Almost there!!

3. Using our test fixtures

To consume FakeCustomerServicein any other module, you just need to add a textFixtures dependency in the consumer module

testImplementation(testFixtures(project(":mylib")))

Test fixtures are directly available in the module they are exposed from so no extra ceremony is required for them.

And that’s it!!

Thanks for reading!

If you found this article helpful, please consider giving it claps 👏🏻 and follow for more useful snapshots.

--

--