Robolectric: Sometimes tests take a very long time to run tests in Android Studio

Created on 27 Jul 2017  路  3Comments  路  Source: robolectric/robolectric

Description

Sometimes tests take a very long time to run in Android Studio. 2+ minutes, and other times only ~7 seconds.

I've recently added Robolectric to my teams pure Kotlin application. We have a handful of Android JUnit tests as well, but needed something more powerful. In using the current Android Studio version (2.3.3) In adding my first test I noticed that while running the tests in Android Studio (I haven't reproduced this while running with ./gradlew test), about 25% of the time, the tests will hang and take an extraordinarily long time to run. A test that would normally run in 7 seconds, can sometimes take 2+ minutes for no clear reason.

I can tell Android studio to run a single class of tests, with a single test that takes 1-2 minutes:

@Test fun exampleTest() {
  println("Test is now running")
}
  1. This doesn't seem to happen with the latest version of Android Studio, 3.0.0 Canary 8. I'm not changing my Android plugin version, just changing IDEs
  2. Sometimes the 3.0.0 Canary 8 will lag when I'm doing something with Activities I've noticed, and it exhibits the same behavior where it sometimes lags, taking 1-2 minutes to run the test for no apparent reason. In this case, the test will actually begin, and then lag when he Activity is being started up. Again, this happens ~25% of the time.
Robolectric.buildActivity(MainActivity::class.java)
    .create()
    .start()
    .resume()
    .visible()
    .get()

Robolectric & Android Version

Android Plugin 2.3.3
Robolectric 3.3.2
Compile SDK 25

Most helpful comment

I believe this issue is related to using Robolectric with PowerMock at the same time. When I removed powermock, my tests are consistently fast.

EDIT: It runs fast until I start using the ActivityLifecycle.

All 3 comments

I believe this issue is related to using Robolectric with PowerMock at the same time. When I removed powermock, my tests are consistently fast.

EDIT: It runs fast until I start using the ActivityLifecycle.

I can confirm this issue. Using Android Studio 3.0, i have a very very simple test that starts a fragment and verifies some textfields on it. In Android Studio, it takes like 4 Minutes (!!!!!!!) to execute, in the console it runs in under 5 seconds.
Using Robolectric 3.5.1

I also noticed that using the incubating mock-maker-inline feature from Mockito slows down my tests by about 30%.

Was this page helpful?
0 / 5 - 0 ratings