@@ -4,7 +4,7 @@ In version 1.6.0, the API of the test module changed significantly.
4
4
This is a guide for gradually adapting the existing test code to the new API.
5
5
This guide is written step-by-step; the idea is to separate the migration into several sets of small changes.
6
6
7
- ## Remove custom ` UncaughtExceptionCaptor ` , ` DelayController ` , and ` TestCoroutineScope ` implementations
7
+ ## Remove custom UncaughtExceptionCaptor, DelayController, and TestCoroutineScope implementations
8
8
9
9
We couldn't find any code that defined new implementations of these interfaces, so they are deprecated. It's likely that
10
10
you don't need to do anything for this section.
@@ -61,7 +61,7 @@ So, there could be two reasons for defining a custom implementation:
61
61
* Using without ` runBlockingTest ` . In this case, you don't even need to implement ` TestCoroutineScope ` : nothing else
62
62
accepts a ` TestCoroutineScope ` specifically as an argument.
63
63
64
- ## Remove usages of ` TestCoroutineExceptionHandler ` and ` TestCoroutineScope.uncaughtExceptions `
64
+ ## Remove usages of TestCoroutineExceptionHandler and TestCoroutineScope.uncaughtExceptions
65
65
66
66
It is already illegal to use a ` TestCoroutineScope ` without performing ` cleanupTestCoroutines ` , so the valid uses of
67
67
` TestCoroutineExceptionHandler ` include:
@@ -93,13 +93,13 @@ fun testFoo() = runTest {
93
93
}
94
94
```
95
95
96
- ## Auto-replace ` TestCoroutineScope ` constructor function with ` createTestCoroutineScope `
96
+ ## Auto-replace TestCoroutineScope constructor function with createTestCoroutineScope
97
97
98
98
This should not break anything, as ` TestCoroutineScope ` is now defined in terms of ` createTestCoroutineScope ` .
99
99
If it does break something, it means that you already supplied a ` TestCoroutineScheduler ` to some scope; in this case,
100
100
also pass this scheduler as the argument to the dispatcher.
101
101
102
- ## Replace usages of ` pauseDispatcher ` and ` resumeDispatcher ` with a ` StandardTestDispatcher `
102
+ ## Replace usages of pauseDispatcher and resumeDispatcher with a StandardTestDispatcher
103
103
104
104
* In places where ` pauseDispatcher ` in its block form is called, replace it with a call to
105
105
` withContext(StandardTestDispatcher(testScheduler)) `
@@ -120,7 +120,7 @@ also pass this scheduler as the argument to the dispatcher.
120
120
` StandardTestDispatcher ` (where dispatches are needed) and ` UnconfinedTestDispatcher ` (where it isn't important where
121
121
execution happens).
122
122
123
- ## Replace ` advanceTimeBy(n) ` with ` advanceTimeBy(n); runCurrent() `
123
+ ## Replace advanceTimeBy(n) with advanceTimeBy(n); runCurrent()
124
124
125
125
For ` TestCoroutineScope ` and ` DelayController ` , the ` advanceTimeBy ` method is deprecated.
126
126
It is not deprecated for ` TestCoroutineScheduler ` and ` TestScope ` , but has a different meaning: it does not run the
@@ -131,7 +131,7 @@ There is an automatic replacement for this deprecation, which produces correct b
131
131
Alternatively, you can wait until replacing ` TestCoroutineScope ` with ` TestScope ` : it's possible that you will not
132
132
encounter this edge case.
133
133
134
- ## Replace ` runBlockingTest ` with ` runTest(UnconfinedTestDispatcher()) `
134
+ ## Replace runBlockingTest with runTest(UnconfinedTestDispatcher())
135
135
136
136
This is a major change, affecting many things, and can be done in parallel with replacing ` TestCoroutineScope ` with
137
137
` TestScope ` .
@@ -329,7 +329,7 @@ There is a `runTestWithLegacyScope` method that allows migrating from `runBlocki
329
329
from ` TestCoroutineScope ` to ` TestScope ` , if exactly the ` TestCoroutineScope ` needs to be passed somewhere else and
330
330
` TestScope ` will not suffice.
331
331
332
- ## Replace ` TestCoroutineScope.cleanupTestCoroutines ` with ` runTest `
332
+ ## Replace TestCoroutineScope.cleanupTestCoroutines with runTest
333
333
334
334
Likely can be done together with the next step.
335
335
@@ -363,7 +363,7 @@ fun runTestAndCleanup(body: TestScope.() -> Unit) = runTest {
363
363
}
364
364
```
365
365
366
- ## Replace ` runBlockingTest ` with ` runBlockingTestOnTestScope ` , ` createTestCoroutineScope ` with ` TestScope `
366
+ ## Replace runBlockingTest with runBlockingTestOnTestScope, createTestCoroutineScope with TestScope
367
367
368
368
Also, replace ` runTestWithLegacyScope ` with just ` runTest ` .
369
369
All of this can be done in parallel with replacing ` runBlockingTest ` with ` runTest ` .
@@ -379,13 +379,13 @@ handle cancelled tasks differently: if there are *cancelled* jobs pending at the
379
379
Of all the methods supported by ` TestCoroutineScope ` , only ` cleanupTestCoroutines ` is not provided on ` TestScope ` ,
380
380
and its usages should have been removed during the previous step.
381
381
382
- ## Replace ` runBlocking ` with ` runTest `
382
+ ## Replace runBlocking with runTest
383
383
384
384
Now that ` runTest ` works properly with asynchronous completions, ` runBlocking ` is only occasionally useful.
385
385
As is, most uses of ` runBlocking ` in tests come from the need to interact with dispatchers that execute on other
386
386
threads, like ` Dispatchers.IO ` or ` Dispatchers.Default ` .
387
387
388
- ## Replace ` TestCoroutineDispatcher ` with ` UnconfinedTestDispatcher ` and ` StandardTestDispatcher `
388
+ ## Replace TestCoroutineDispatcher with UnconfinedTestDispatcher and StandardTestDispatcher
389
389
390
390
` TestCoroutineDispatcher ` is a dispatcher with two modes:
391
391
* ("unpaused") Almost (but not quite) unconfined, with the ability to eagerly enter ` launch ` and ` async ` blocks.
0 commit comments