57

I'm trying to replace my Espresso registerIdlingResources and unregisterIdlingResources deprecated method by using IdlingRegistry method according to Android documentation.

Some of my tests worked before the update and no longer work now. These tests work unitarily, but not together.

I noticed that there is a little difference with the old version (of Espresso class), this line is not present in IdlingRegistry class :

baseRegistry.sync(IdlingRegistry.getInstance().getResources(), IdlingRegistry.getInstance().getLoopers());

I think this sync method is very important for my custom IdlingResource...

How can I sync my looper without this line?

Edit: I use EspressoCore 3.0.1 with runner/rules 1.0.1

Edit2: Link of documentation who has been specify deprecation : Here and Here.

Tot Zam
  • 8,406
  • 10
  • 51
  • 76
Crisic
  • 1,181
  • 1
  • 9
  • 27

2 Answers2

1

1. Android Test Orchestrator.

Android Test Orchestrator resolves your issue with IdlingResources test fails.

dependencies {
  androidTestUtil 'androidx.test:orchestrator:1.3.0'
}

Configure your build.gradle:

android {
 defaultConfig {
   testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
   testInstrumentationRunnerArguments clearPackageData: 'true' 
 }
 testOptions {
   execution 'ANDROIDX_TEST_ORCHESTRATOR'
 }
}

Then run your tests:

./gradlew connectedCheck

2. Refactor the code

The issue IdlingResources cause tests to fail. Refactoring solves such problems.

  • Use Thread.sleep() instead of IdlingResources, I personally don't recommend in High Level Programming but it is fixes the problem.
  • Modify the code with Callbacks & Hooks.
  • Simplify the code without many and complex dependencies.

3. Use Deprecated Methods.

registerIdlingResources and unregisterIdlingResources Deprecated Methods are working in this context. Note that these methods need updates in the future versions of Espresso.

4. Use Third-party Testing Library.

I would give a try also with Robolectric or Appium. They can give good guidance to handle IdlingResources, along with performance benefits around testing environments.

5. Upgrade to latest Espresso library:

androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

6. Define IdlingResource:

 private val idlingResource = CountingIdlingResource("idlingResource")

7. Register IdlingResource:

 IdlingRegistry.getInstance().register(idlingResource)

8. Unregister IdlingResource:

 IdlingRegistry.getInstance().unregister(idlingResource)

9. Synchronize the Looper:

Handler(Looper ... ()).Delayed({

    // Your code

}, ...)
Sara Iorgulescu
  • 7
  • 1
  • 1
  • 6
0

Be sure to run the latest version of androidx.test

If your tests run one at a time, but fail when run together, Android Test Orchestrator ("ATO") can solve that problem. ATO runs each test method in a new process, so any in memory state is cleared.

From the docs, the basic gradle setup is:

android {
  defaultConfig {
   ...
   testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

   // The following argument makes the Android Test Orchestrator run its
   // "pm clear" command after each test invocation. This command ensures
   // that the app's state is completely cleared between tests.
   testInstrumentationRunnerArguments clearPackageData: 'true'
 }

  testOptions {
    execution 'ANDROIDX_TEST_ORCHESTRATOR'
  }
}

dependencies {
  androidTestImplementation 'androidx.test:runner:1.3.0'
  androidTestUtil 'androidx.test:orchestrator:1.3.0'
}

The docs also include setup for installing and using Android Test Orchestrator without gradle.

You can also use ATO on Firebase Test Lab:

If you still have problems with your IdlingResources you can try the busybee library which simplifies using IdlingResources and makes them easier to debug. (disclaimer, I am a maintainer of that library)

yogurtearl
  • 3,035
  • 18
  • 24