Avoid using `runBlocking` in tests

Is there a way to avoid using runBlocking in tests?

I currently have the following:

class ServerTest {

  @Test fun testLocalServer() = runBlocking {
	...
  }

  @Test fun testStagedServer() = runBlocking {
	...
  }
}

I would like this to somehow become:

  @Test fun testLocalServer() {
	...
  }

  @Test fun testStagedServer()  {
	...
  }

Basically, I want my code to look like the second code block but still behave exactly like the first code block. I am assuming that I will want every single one of my tests to run inside of a runBlocking block, so it’s redundant to keep writing it.

Make sure to include testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.4")
Sadly, @Test suspend fun is not planned to be supported. Instead, you’re meant to use runTest instead of runBlocking

2 Likes

Thanks @kyay10 !

1 Like