Hi! I am learning Kotlin and I've never worked wit...
# kotlintest
j
Hi! I am learning Kotlin and I've never worked with the Java platform before, so I am as noob as they come in that regard. I've made a unit test with Kotlintest but when I run it it seems Gradle doesn't find the test?
Copy code
Execution failed for task ':test'.
> No tests found for given includes: [com.projectfaceoff.rink.entities.EntityManagerTest](filter.includeTestsMatching)
I'm sure I've missed something in the setup or something or placed the test file in the wrong place, KotlinTest docs don't say much about those things expect how to config the gradle build file. I have a class extended with
BehaviorSpec
inside a
test/kotlin/com.foo.bar/
folder and created a
Given
When
Then
test.
u
Is it in perhaps in github or somewhere else where one could access it? Would be easier if we could take a look at the code and the folder structure.
I've moved the test file to the root of
src/test/kotlin
to see if there was an issue with that, so that's why it isn't the same as in the error message above.
u
Could you try adding the following to your `build.gradle.kts`:
Copy code
plugins {
    kotlin("jvm") version "1.3.50"
}

repositories {
    jcenter()
}
s
Upgrade to 3.4.1 as well as that's the latest version
gradle check
should be enough to run that test
j
Sorry for not posting the entire build file. I already have the plugins bit and my repositories uses mavenCentral
Does that matter?
Updated the gist with the whole build file
u
It’s easier to understand what’s wrong if we can see the whole picture
j
Well if I'd ask you this then, What does it take to setup a project for unit testing with KotlinTest? Where should a test file exist and what should it look like?
I think it's probably as easy as answering those three questions
s
Yes, that's a good question
But that's using gradle groovy not gradle kotlin
It's also a bit out of date but you should be able to clone and run it
j
That's pretty much what I've already done. I tried to figure out how a test should be written looking at the samples. My gist has a sample test class using the BehaviorSpec
Is there some convention regarding where test files should be located?
s
anywhere in src/test/kotlin should be fine
j
I assumed anywhere under test/kotlin
s
Let me create this project like yours and run it
what version of gradle ?
j
Uhm don't know. Using intellij so I haven't set it up myself. Don't even think I have gradle in my path variable... 🤔
s
looks like the issue is test support isn't being added
j
What does that mean? 🙂
s
well when I import your project into intellij there's no gradle check task
so it's not finding the plugins
I'm guessing when you use gradle.kts rather than build.gradle you need to do something different
I'm trying to figure it out
That works
Clone the project and then do
cd jvm-gradle
./gradlew check
j
There's nothing called check to run in that folder?
s
check is a gradle task
It will run unit tests from kotlin
you can do
./gradlew test
too
The command is gradlew not gradle, I updated it
j
OK, one moment. Apparently I'm missing the JAVA_HOME env variable
s
ok
I've added 3 example projects now to https://github.com/kotlintest/kotlintest-example-projects 1. jvm-gradle an example of using kotlintest with build.gradle 2. jvm-gradle-kts an example of kt with build.gradle.kts (what you want) 3. multiplatform an example mpp project
👍 1
j
Okay so I noticed a difference in your build.gradle file and mine so I changed
Copy code
tasks.getting(Test::class) {
    useJUnitPlatform { }
}
to
Copy code
tasks.test {
    useJUnitPlatform { }
}
And it worked
Test is now being tested
Is it because I didn't do exactly like the instructions say?
Copy code
val test by tasks.getting(Test::class) {
    useJUnitPlatform { }
}
That worked too. I knew it was probably a configuration error on my part
@sam Thanks for the help
s
You're welcome
And welcome to the KotlinTest family 😁
j
Cheers 🙂 Kotlin is pretty sweet