Ahh ... it looks like when tests are excuted via g...
# kotest
t
Ahh ... it looks like when tests are excuted via gradle + cli my custom AbstractProjectConfig isn't being executed ... is that normal behaviour? I had to look at the stack trace to see that my tests were never running due to missing env variables which im currently setting in my PorjectConfig.
s
Should work regardless if its on the classpath
t
I must be missing something 🤔 It works as expected when I run:
gradle :project:test --tests "com.example.MyTest"
but the following fail (because there are no env vars set)
gradle :project:test --tests "com.example.*"
or
gradle :project:test
or
gradle :project:check
??
s
Is the config in the same module as the test
t
yes
s
Is the first one definitely running the test and not skipping it
t
s
Put an init block in your project config and in that init block throw an exception. Then run gradle check and see if it errors out
t
two sec
gradle :cloud:test --tests "io.glimpseprotocol.cloud.SingleVaultTest"
fails when the init block throws
s
Ok I think your error is that you need the use junit block in every gradle file or gradle gets confused
The useJunitPlatform thing
t
ahh
2 sec
doesn't appear to have had any effect
s
I'm pretty sure that's the cause
A test block in every module including root with useJunitPlatform
t
🤷‍♂️
s
Humour me and make the tests block top level in each file
t
👍
No joy 😞
In case it helps here are the two files in question
s
I had this exact error on kotest itself for a while and couldn't figure it out. Then I found that it was because gradle would find no tests (because no test engine was configured) and would report that as an error.
Ahh I have this in kotest as well
filter { isFailOnNoMatchingTests = false }
t
Still failing, but slightly different error message after adding the filter block (top is without the filter block)
Here's the stack trace if thats helpful
s
GLIMPSE_SERVICE_NAME must not be null
t
That value is set inside TestConfig's AbstractProjectConfig block ...
... which brings us back to my initial comment in the main channel 😂
s
lol
well now we've fixed the gradle check thing
can you show me your config class
t
Copy code
package io.glimpseprotocol.cloud

import io.kotest.core.config.AbstractProjectConfig
import io.kotest.core.listeners.Listener
import io.kotest.extensions.system.SystemEnvironmentProjectListener

class ProjectConfig : AbstractProjectConfig() {
//    init {
//        error("I'm an error!!!!")
//    }

    override fun listeners(): List<Listener> = listOf(
        SystemEnvironmentProjectListener(
            mapOf(
                "GLIMPSE_ENV" to "test",
                "GLIMPSE_SERVICE_NAME" to "cloud",
                "GLIMPSE_VERSION" to "x.x.x-test",
                "GLIMPSE_PORT" to "20001",
                "GLIMPSE_DOMAIN" to "localhost",
                "GLIMPSE_ROOT_PATH" to "/cloud/v1",
                "GLIMPSE_COOKIE_ENCRYPT" to "covfefeecovfefee",
                "GLIMPSE_COOKIE_SIGN" to "covfefeecovfefee",
                "GLIMPSE_MONGO_URI" to "<mongodb://127.0.0.1:27017/?readPreference=primary&ssl=false>",
                "GLIMPSE_LOGGING_LEVEL" to "debug"
            )
        )
    )
}
s
I think what's happening is the env var is trying to be used before the listeners are firing to set the env vars. Do you have that System.envVar(GLIMPSE_SERVICE_NAME) in a static initializer block
t
i do yes
s
Ok, so gradle will find your test classes first, which causes those init blocks to fire, then passes the classes over to the test engine, in this case kotest
note: static init block is not the same as an init block in a class
t
I assuem you mean something like this:
s
anything in an object will be executed as soon as the class is loaded by the classloader
so way before kotest can get involved
t
makes sense
hmm how to get around 🤔
s
so you could maybe make it lazy
t
ya
could I use lazy?
s
you would need to make the private val lazy
and the object env might need tweaking as well, not sure
t
okay i'll have to play around with this and see if I can get it to work a bit later running out of time before the weekend 🙂 I assume I could just set the env variables inside gradle? Not ideal but could be a workround in the interim. Thank you for your help though 🙏
s
Yes you could do that
and probably is better anyway as env vars ideally should be set before the program starts - setting them in kotest is a bit of a hack as it has to use reflection to change the map
t
I can't recall the reason I put them there, but I just started using kotest like a week ago so 🤷‍♂️
s
There's nothing super bad about it, but I think it's better if it's in the gradle file or whatever.
The kotest env listeners are really intended where you want to tweak an env var while running in order to test that something changes with different values.
t
I prefer them in gradle as well ... they were there a few days ago but I can't recall why I moved them into the test config 🤔
Hey @sam sorry for being a pain. I've tried moving environment variables into build.gradle.kts but now the kotest extension doesn't pickup the variables (iirc this is why I originally used
SystemEnvironmentProjectListener)
. Should kotest be picking up env varialbes from build.gradle.kts? If not, how do people reconcile thier cli test running from the idea test running?
s
kotest picks up env from the shell like any program. My guess is gradle isn't passing them through (forked process?)
t
hmm thats annoying!
i'll take a look at SO and see how striaghtforward it is to expore them into the forked process
thanks!
s
looks like its fairly easy
or you could include them in whatever script you use to start it
t
ahh ya seems striaghtforward