Skip to content

Instrumentation Testing

Daniel Molinero edited this page Oct 16, 2017 · 3 revisions

Android Instrumentation Testing Complications

The following isn't needed if you are using Android Test Orchestrator. Orchestrator uses different application instances for each test, so tests using the orchestrator can be written in the same way as unit tests.

One of the best benefits of using dependency injection, is replacing dependencies when testing. Toothpick makes this very easy for unit tests, and we will see it makes it easy to create instrumentation tests as well, but a bit different.

In unit tests, every tests is independent. Hence, it is possible to create scopes for a tests, and wipe them out between 2 tests. On the other hand, Android instrumentation tests reuse the same Application instance when running multiple tests. In Toothpick, we usually create one scope for the application. Hence, we can't wipe out the application scope between tests because we actually want to reuse the scope in 2 different tests, but with different test modules.

To achieve this in Toothpick, simply call Toothpick.reset(appScope) and reinitialize it with the appropriate test modules for each test.

Solving it by Resetting Scopes

Toothpick v1.1.1 introduced the ability to reset individual scopes. This clears any modules and test modules, which enables each test to add them at their start. Flow for instrumentation tests:

  1. In @Before or the activity is launched, install the appropriate test modules
  2. Test runs with test modules
  3. In @After reset application scope and reinstall modules

Reinstalling modules into the appropriate scope in the last step is important. Resetting the scope clears everything, so for the next test to run successfully you need to reinstall the modules as if it's the first time the scope is being created. This can be achieved by exposing the scope setup logic via a method callable from the tests.

Example

An example has been created as part of the Smoothie Sample application. See SimpleActivityTest for how the above flow was implemented to provide concrete as well as mocked dependencies for instrumentation tests.

Gotchas

Resetting solves the problem of using Toothpick test modules with instrumentation tests, but there are a few gotchas to be aware of when using this approach.

WIP