-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support test methods with Kotlin suspend modifier #1914
Comments
Related to #1851 |
@marcphilipp Yes, not resolved, this is different issues |
Please resolve this! My tests looked all fine until I discovered that they were not run at all anymore after adding suspending functions :/ |
An alternate work around is to create a wrapper around fun suspendingTest(context: CoroutineContext = EmptyCoroutineContext, block: suspend CoroutineScope.() -> Any): Unit {
runBlocking(context, block)
Unit
} That will allow tests to use suspending methods: @Test
fun `a suspending test should compile and be found`() = suspendingTest {
assertThat(thing()).isEqualTo("a string")
}
suspend fun thing() = "a string" Would adding that |
Such function is available in kotlinx-coroutines-test package. It’s called runBlockingTest
…Sent from my iPhone
On 7 Oct 2021, at 23:10, checketts ***@***.***> wrote:
An alternate work around is to create a wrapper around runBlocking() that returns Unit
fun suspendingTest(context: CoroutineContext = EmptyCoroutineContext, block: suspend CoroutineScope.() -> Any): Unit {
runBlocking(context, block)
Unit
}
That will allow tests to use suspending methods:
@test
fun `a suspending test should compile and be found`() = suspendingTest {
assertThat(thing()).isEqualTo("a string")
}
suspend fun thing() = "a string"
Would adding that suspendingTest method be useful for JUnit or would supporting tests with suspend be preferred?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or unsubscribe.
Triage notifications on the go with GitHub Mobile for iOS or Android.
|
@IRus Thanks for pointing that out! At least now this issue documents it as an option. |
I would be careful with using runBlockingTest. It does a lot of things, one of them is overriding the behavior of delay(), to allow the test to control the passage of time across coroutines. By default it makes all calls to delay() a no-op. Anyone who needs to wait in a test will get unexpected behavior if they use delay (which would be the recommended way of doing it). |
Might be interesting to know that I implemented @IRus' suggestion in jqwik.net. With the extension point (aka lifecycle hook) for invokeMethods present, the implementation is rather simple: https://github.com/jlink/jqwik/blob/main/kotlin/src/main/kotlin/net/jqwik/kotlin/internal/SuspendedPropertyMethodsHook.kt @IRus I did not implement parameter resolution for CoroutineScope and TestCoroutineScope. In which cases would these be necessary? |
@jlink I wonder how you work around discovery issue: Scope is nice to have to set test dispatcher by default to |
jqwik doesn't have that problem since it allows any return type in test/property methods. |
So it's an engine on its own, ok |
This is a recurrent source of bugs for us. |
I have discovered a way to make suspend work with JUnit Jupiter tests by bridging them through a |
I also ran into this because I had the idea of trying to write a (I suspect this limitation also explains the cumbersome |
For anyone coming from a search engine, this is still not supported, but it looks possible using |
Has anyone tried using the new |
I am using it. Adding |
Yes runTest works fine. It’s just rather confusing that it’s not supported out of the box at this point. |
... until you need to integrate with Lincheck and would like to run suspending set up code in |
Goals
Support running suspend test in JUnit Jupiter:
Currently, such test can be written this way:
Also, will be nice to provide
CoroutineScope
through params, or as receiver in extension:1 and 2 actually the same on bytecode level.
suspend
is optional.And finally, support for
runBlockingTest
:What can be done currently
ParameterResolver
can be used to provide stubs forContinuation
,CoroutineScope
andTestCoroutineScope
. These stub arguments can be replaced with real arguments in invocation.Problems
Current extensions points not enough to implement this feature as extensions, since:
void
, butsuspend fun
returnsObject
;InvocationInterceptor
in 5.5-M1(SNAPSHOT) don't providing mechanism to override actual invocation, only to decoration of existing invocation. Conversion ofmethod
tokotlinFunction
, and then executing usingcallSuspend
is necessary to executesuspend fun
.Also, my slides about this topic.
The text was updated successfully, but these errors were encountered: