I'm trying to follow the instructions here (<https...
# gradle
c
I'm trying to follow the instructions here (https://kotlinlang.org/docs/jvm-test-using-junit.html#add-dependencies) to add testing to my Kotlin project, but they just don't work. First, it never explains which
build.gradle.kts
I'm supposed to editing. Second, it says
test
and
useJUnitPlatform
are unresolved references. How is this supposed to work?
v
If those are unresolved, you are probably in the wrong build script. The instructions can hardly tell you "which" build script as it does not know your project structure. So just take it logically, that project where you also apply the Kotlin plugin, because that is the project where you want to add tests.
c
There is one build script at the root level of my project, and one in the
composeApp
directory. I have tried adding the code to both. Neither works.
The top build script is apparently just to disable a bunch of plugins which the lower build script enables.
v
Well hard to guess without you showing your scripts.
The top build script is apparently just to disable a bunch of plugins which the lower build script enables.
Unlikely, as that is not possible and does not make any sense either :-)
c
Copy code
plugins {
    // this is necessary to avoid the plugins to be loaded multiple times
    // in each subproject's classloader
    alias(libs.plugins.androidApplication) apply false
    alias(libs.plugins.androidLibrary) apply false
 ...
These are all the scripts generated by https://kmp.jetbrains.com/ — there's very minor changes applied.
It's probably worth noting that wizard doesn't create a
Copy code
plugins {
 kotlin("jvm") version "2.1.0"
}
section.
v
That snippet is not disabling anything. It adds these plugins to the buildscript classpath (and thus to a common classloader of all subprojects) without applying them.
Also, as you now mentioned this is a KMP project, that explains that it does not work. The doc you linked to is for normal K/JVM, not for KMP.
test
would then be
jvmTest
for example for the JVM part of the KMP project
But that will probably also not resolve, as no accessors are generated for these tasks, as Gradle cannot know which targets of the KMP you enable
So in KMP you would for the JVM part more do something like
Copy code
kotlin {
    jvm {
        val test by testRuns.existing {
            executionTask {
                useJUnitPlatform()
            }
        }
    }
}
👍 1
c
That worked, but the rest of the tutorial is broken for me. I can choose "Generate Tests" in my IDE as it says, but it can't find any testing libraries, clicking "Fix" for that problem does claim to fix it although closing and reopening the dialog complains about the problem again, and if I click "OK" to generate the tests nothing happens.
Is there a tutorial somewhere that actually targets the kind of Kotlin project this is and explains what each step is doing so I have a chance of figuring out why things are going wrong and can learn something?
I feel like I'm blindly following each step here and each step is failing with no explanation or ability for me to debug it.
c
you are in the wrong tutorial. the link you posted is for a simple Kotlin JVM project. you are using Kotlin Multiplatform which is a whole different story in regards to gradle config and IDE plugins. From you other posts it seem that you are using Android Studio which will not know how to add the test dependencies in your Multiplatform project as it’s focus is Android development.
And the IDE they are using in the tutorials is IntelliJ IDEA.
c
Are you sure?
c
c
Yes. Well, except for Xcode and KDoctor.
👍🏼 1
t
fyi @Sarah Haggarty
👀 1
s
Hi @Chris B!
c
Hi
s
To prevent this from happening to other users in future, can you let me know how you found this page originally?
c
I probably searched for "kotlin unit tests android studio" and since all the results were all for testing android devices changed it to "kotlin unit tests studio"
s
Thanks for your quick feedback! I'll take a look and see what we can do here.
c
Thanks!
The biggest problem is that there are dozens and dozens of documents on how to implement unit testing in Kotlin projects, both official and unofficial. And they all do different things — there's at least five different testing frameworks, multiple build systems with incompatible files, half use IntelliJ and half use Android Studio. I'm still completely in the dark about what the differences really are, and which ones matter.
s
Thanks for sharing your thoughts! I can see how all those different options and setups could feel overwhelming. It’s definitely an area where some clarity would improve things. I’ll make sure your feedback gets noted!
👍 1
Hi there! As a first step, we've added a note at the beginning of our Test code using JUnit in JVM - tutorial, referencing our Kotlin Multiplatform tutorial for anyone who finds this page and who is working on a multiplatform app.
👍 1