Coder Social home page Coder Social logo

Comments (5)

dkhalanskyjb avatar dkhalanskyjb commented on June 26, 2024

There's a potential for the documentation to be improved, but yes, it's not enough to replace the Main dispatcher with a TestDispatcher: someone has to actually execute the tasks on that dispatcher (that is, something should emulate the event loop of the main thread), and runTest is the one doing it. It's certainly not enough to just override the main dispatcher, even if your coroutines don't throw exceptions: some tasks spawned on the main thread just won't run.

In theory, you can use a TestDispatcher with a TestCoroutineScheduler separately from runTest, but this is a very advanced use case that requires a deep understanding, so their docs certainly should mention runTest.

from kotlinx.coroutines.

SnyersK avatar SnyersK commented on June 26, 2024

Would there be a way to detect if a test is not using runTest when required, and throw an exception, so it's immediately clear that this is missing?

(Or an existing test needs to be updated, after a change)

I don't know if the TestDispatcher can perhaps check if it's running in a TestScope, for example?

from kotlinx.coroutines.

dkhalanskyjb avatar dkhalanskyjb commented on June 26, 2024

Unfortunately, no: injecting a TestDispatcher before a TestScope exists or runTest is run is an important pattern. Dependency injection is typically initialized before the test is started.

from kotlinx.coroutines.

SnyersK avatar SnyersK commented on June 26, 2024

Ah yes, I understand now.
Classes create their own / use another CoroutineScope.
There's no link between the TestScope and the coroutines launched from it's scope.

I've found that apparently there's a Detekt rule to check if tests which interact with coroutines make use of runTest.
Will need to try this out.

https://github.com/detekt/detekt/blob/f8bf79b24baebd10fda01ffe2ef586cf8bc1fff1/detekt-core/src/main/resources/default-detekt-config.yml#L175-L178

from kotlinx.coroutines.

SnyersK avatar SnyersK commented on June 26, 2024

It's safe to assume that, as a general rule of thumb:

all of the tests in a test class which replaces the Main scheduler; and injects the TestDispatcher for all of the other dispatchers; should use runTest, to avoid running into potential (future) issues, even if the test itself is not directly interacting with a coroutine / using the time methods, right?

So even if a test just calls a method which is not launching any coroutine at all, it would be safer to just use runTest regardless?

from kotlinx.coroutines.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.