CoroutineTest

open class CoroutineTest(    context: CoroutineContext = EmptyCoroutineContext,     testDispatcherBuilder: (TestCoroutineScheduler) -> TestDispatcher = { StandardTestDispatcher(it) }) : CoroutineTestRule, AttachedDisposables

Base class for unit testing coroutine based code.

On every test run this class automatically sets MainScope and dispatchers to use a TestCoroutineDispatcher.

Internally, this uses a CoroutineTestRule which does the actual dispatchers and Dispatchers.setMain() setup. This allows accessing e.g. the testScope in your @Before setup method.

Moreover, this provides an attachedDisposables attribute and a collectFlow helper, so you can activate SharingStarted.WhileSubscribed based flows created with derived, for example.

Inheritors

Constructors

Link copied to clipboard
constructor(context: CoroutineContext = EmptyCoroutineContext, testDispatcherBuilder: (TestCoroutineScheduler) -> TestDispatcher = { StandardTestDispatcher(it) })

Properties

Link copied to clipboard
Link copied to clipboard
val testDispatcher: TestDispatcher
Link copied to clipboard
val testScope: TestScope

Functions

Link copied to clipboard
fun <T> TestScope.collectFlow(flow: Flow<T>, collector: suspend (T) -> Unit = {})

Collects a flow in the background.

Link copied to clipboard
open override fun dispose()
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
open override fun runTest(block: suspend TestScope.() -> Unit): TestResult

In addition to running the test this also disposes the attachedDisposables (useful with collectFlow).