fun TestScope(context: CoroutineContext = EmptyCoroutineContext): TestScope

Creates a TestScope.

It ensures that all the test module machinery is properly initialized.

  • If context doesn't provide a TestCoroutineScheduler for orchestrating the virtual time used for delay-skipping, a new one is created, unless either

  • If context doesn't have a TestDispatcher, a StandardTestDispatcher is created.

  • A CoroutineExceptionHandler is created that makes TestCoroutineScope.cleanupTestCoroutines throw if there were any uncaught exceptions, or forwards the exceptions further in a platform-specific manner if the cleanup was already performed when an exception happened. Passing a CoroutineExceptionHandler is illegal, unless it's an UncaughtExceptionCaptor, in which case the behavior is preserved for the time being for backward compatibility. If you need to have a specific CoroutineExceptionHandler, please pass it to launch on an already-created TestCoroutineScope and share your use case at our issue tracker.

  • If context provides a Job, that job is used as a parent for the new scope.


if context has an CoroutineExceptionHandler that is not an UncaughtExceptionCaptor.