Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Coroutines Test 入門 / Android Test Night #8

Coroutines Test 入門 / Android Test Night #8

star_zero

March 10, 2023
Tweet

More Decks by star_zero

Other Decks in Programming

Transcript

  1. class SampleTest { @Test fun testRun() = runTest { val

    sample = Sample() val result = sample.run() assert(result == "hoge") } }
  2. class SampleTest { @Test fun testRun() = runTest { val

    sample = Sample() val result = sample.run() assert(result == "hoge") } }
  3. class SampleTest { @Test fun testRun() = runTest { val

    sample = Sample() val result = sample.run() assert(result == "hoge") } }
  4. @Test fun test() = runTest { var result = 0

    launch { result = 1 } assert(result == 1) }
  5. @Test fun test() = runTest { var result = 0

    launch { result = 1 } assert(result == 1) } ❌ Failed
  6. @Test fun test() = runTest { var result = 0

    launch { result = 1 } assert(result == 1) } ① ② ③
  7. @Test fun test() = runTest { var result = 0

    launch { result = 1 } runCurrent() assert(result == 1) } ✅ Success
  8. @Test fun test() = runTest { var result = 0

    launch { result = 1 } runCurrent() assert(result == 1) } ① ② ③ ④
  9. @Test fun test() = runTest { var result = 0

    launch { delay(1000) result = 1 } runCurrent() assert(result == 1) }
  10. @Test fun test() = runTest { var result = 0

    launch { delay(1000) result = 1 } runCurrent() assert(result == 1) } ❌ Failed
  11. @Test fun test() = runTest { var result = 0

    launch { delay(1000) result = 1 } runCurrent() assert(result == 1) } ① ② ③ ④ ⑤
  12. @Test fun test() = runTest { // ... advanceUntilIdle() //

    または advanceTimeBy(1001) assert(result == 1) } ✅ Success
  13. @Test fun test() = runTest(UnconfinedTestDispatcher()) { var result = 0

    launch { result = 1 } assert(result == 1) } ✅ Success
  14. @Test fun testRun() = runTest { val sample = Sample()

    val result = sample.run() assert(result == "hoge") }
  15. @Test fun testRun() = runTest { val sample = Sample()

    val result = sample.run() assert(result == "hoge") }
  16. @Test fun test() = runTest(UnconfinedTestDispatcher()) { val sample = Sample()

    val result = mutableListOf<Int>() val job = launch { sample.flow.toCollection(result) } assert(result.size == 0) sample.emit(1) assert(result.size == 1) assert(result.first() == 1) job.cancel() }
  17. @Test fun test() = runTest(UnconfinedTestDispatcher()) { val sample = Sample()

    val result = mutableListOf<Int>() val job = launch { sample.flow.toCollection(result) } // ... }
  18. @Test fun test() = runTest(UnconfinedTestDispatcher()) { val sample = Sample()

    val result = mutableListOf<Int>() val job = launch { sample.flow.toCollection(result) } // ... }
  19. @Test fun test() = runTest(UnconfinedTestDispatcher()) { // ... val job

    = launch { sample.flow.toCollection(result) } assert(result.size == 0) // ... }
  20. @Test fun test() = runTest(UnconfinedTestDispatcher()) { // ... val job

    = launch { sample.flow.toCollection(result) } sample.emit(1) // ... }
  21. @Test fun test() = runTest(UnconfinedTestDispatcher()) { // ... val job

    = launch { sample.flow.toCollection(result) } // ... job.cancel() }
  22. @Test fun test() = runTest(UnconfinedTestDispatcher()) { // ... backgroundScope.launch {

    sample.flow.toCollection(result) } // ... // job.cancel() }