Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

298
Views
The TestScope advanced example of usage in tests with coroutines

Could someone provide an advanced example of using the new TestScope and TestScope.launch while testing with coroutines?

It seems like in the new kotlinx.coroutines.test release they've added somewhat called TestScope to the library. Also, they've deprecated the old TestCoroutineDispatcher and told to use TestScope.runTests instead, however, they didn't add much documentation on how to use it. Everything I could find is this.

Can anyone provide me with some extra examples of usage in different scenarios?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I could also fix this problem with @Joffrey mentioned at the comment. You only need to use runTest { } to use coroutine scope at the test code. By using runTest { } you can use TestScope inside the block with this.

In my case, there was a flow function at the viewModel something like this. It was running inside the viewModelScope.

    fun getFriendDataWithFlow() {
        viewModelScope.launch {
            repository.loadFriendsWithFlow()
            ...
        }
     }

And I should test this at my test code, and I used it like this.

   @ExperimentalCoroutinesApi
   @Before
   fun setup() {
       Dispatchers.setMain(StandardTestDispatcher())
   }

   @ExperimentalCoroutinesApi
   @Test
   fun temp() {
        runTest {
            viewModel.getFriendDataWithFlow()
        }
   }

All I did was add runTest { } block, and add coroutine code inside the block. And it worked for me. My test coroutine dependency was "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.0-RC".

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!