question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add a a way to disable automatic delay skipping with runBlockingTest

See original GitHub issue

In many cases it would be useful to accurately control time within tests (for example when testing time-based coroutine logic). However, at the moment runBlockingTest seems to enforce delay-skipping behavior.

There does not seem to be a good alternative to this method at the moment if I don’t want delay skipping. I can manually create TestCoroutineScope but that takes some boilerplate and does not bring all the benefits of runBlockingTest (such as making sure all launched tasks have been properly stopped).

At the moment I’m using copy paste version of runBlockingTest with dispatcher.advanceUntilIdle() removed, but that is really bad solution.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:16 (7 by maintainers)

github_iconTop GitHub Comments

6reactions
matejdrocommented, Jun 10, 2019

Let’s say I have simple method like that:

fun CoroutineScope.showAnimationForTwoSeconds() = launch {
    animationController.startAnimation()

    delay(2000)
    animationController.stopAnimation()
}

Then I want to properly test this:

fun testAnimationStart() = runBlockingTest {
    showAnimationForTwoSeconds()
    
    assertThat(animationController).isAnimationRunning()

    advanceTimeBy(2000)
    assertThat(animationController).isAnimationNotRunning()
}

At the moment this test would fail since runBlockingTest skips all delays automatically, which means that stopAnimation call would be made immediatelly, before test can check whether animation is running.

0reactions
matejdrocommented, May 19, 2021

You are correct. Closing this as duplicate.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unit testing a Kotlin coroutine with delay - Stack Overflow
It allows tests to use the runTest() method and TestScope to test suspending code, automatically skipping delays.
Read more >
Testing Kotlin coroutines on Android
Wrapping your test's code in runTest will work for testing basic suspending functions, and it will automatically skip any delays in coroutines, making...
Read more >
kotlinx-coroutines-test
The calls to delay are automatically skipped, preserving the relative execution order of the tasks. This way, it's possible to make tests finish ......
Read more >
Testing Android Coroutines using runTest - craigrussell
This class adds a lot of numbers to a list, and sorts and shuffles them ... it claims to do in the docs:...
Read more >
Unit Testing with Kotlin Coroutines: The Android Way - Medium
That's because the runBlocking() function can't skip the delay operation. runBlockingTest(). runBlockingTest() is a part of the kotlinx.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found