# Best Practices Testing Coroutines With Delays?

**URL:** <https://discuss.kotlinlang.org/t/best-practices-testing-coroutines-with-delays/25303>\
**Category:** Libraries\
**Created:** [July 27, 2022, 7:50pm UTC](https://discuss.kotlinlang.org/t/best-practices-testing-coroutines-with-delays/25303 "2022-07-27T19:50:13Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![ewingsan](https://avatars.discourse-cdn.com/v4/letter/e/e19b73/32.png) [@ewingsan](https://discuss.kotlinlang.org/u/ewingsan)\
**Post date:** [July 27, 2022, 7:50pm UTC](https://discuss.kotlinlang.org/t/best-practices-testing-coroutines-with-delays/25303/1 "2022-07-27T19:50:14Z")

</div>

If I want to test a function that launches coroutine that has a delay, what are the best practices for testing that function? Is it to use `advanceUntilIdle()`, `advanceTimeBy()`, something else, or some combination depending on the situation?

Here an example setup, which seems to be standard for `org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.x`:

```kotlin
@ExperimentalCoroutinesApi
class MyViewModelTest {
    @get:Rule
    val mainDispatcherRule = TestMainDispatcherRule()

    @get:Rule
    val instantTaskExecutorRule = InstantTaskExecutorRule()

    private lateinit var myViewModel: MyViewModel

    @BeforeTest
    fun setup() {
        myViewModel = MyViewModel(mainDispatcherRule.dispatcher)
    }

    @Test
    fun `test coroutine`() = runTest {
        myViewModel.launchCoroutineWithDelay()
        advanceUntilIdle()
        // or advanceTimeBy(MyViewModel.DELAY_LENGTH)? Or something else?
        assertTrue(myViewModel.coroutineFinished.value ?: false)
    }
}

```

With a ViewModel like

```kotlin
@HiltViewModel
class MyViewModel @Inject constructor(
    @IoDispatcher private val ioDispatcher: CoroutineDispatcher
) {

    val coroutineFinished = MutableLiveData(false)

    fun launchCoroutineWithDelay() {
        viewModelScope.launch(ioDispatcher) {
            delay(DELAY_LENGTH)
            coroutineFinished.postValue(true)
        }
    }

    companion object {
        const val DELAY_LENGTH = 300
    }
}

```

And a TestMainDispatcherRule like

```kotlin
@ExperimentalCoroutinesApi
class TestMainDispatcherRule(val dispatcher: TestDispatcher = StandardTestDispatcher()) : TestWatcher() {

    override fun starting(description: Description?) = Dispatchers.setMain(dispatcher)

    override fun finished(description: Description?) = Dispatchers.resetMain()
}

```
