I am not familiar with testing. So I want to learn...
# getting-started
d
I am not familiar with testing. So I want to learn more in depth about this. For that I need help and guidance. Note: I am doing tests with Kotlin for the first time. I have not done testing in other languages before. This is what I know (What I have already done): • https://www.jetbrains.com/help/idea/tdd-with-kotlin.htmlhttps://kotlinlang.org/docs/jvm-test-using-junit.html (I have followed this) • I have seen few blogs on Mockk an Mokito. • I have seen that JUnit is present in Gradle project by default Problems: when to use what, why to choose one over the other, cannot understand how to customize related stuff in the IDEA IDE, etc
c
Welcome to the world of testing! Many approaches exist, and no one agrees on which are the best. When it comes to test frameworks for Kotlin, my experience is that the only two viable options are either
kotlin.test
(recommended by JetBrains, JUnit-style) or #kotest (much, much more powerful and feature-complete). I recommend you start by learning these two. In my opinion the best explanation of test doubles (mocks…) is this short article, as well as this longer explanation of the benefits of mocks. Note that not everyone agrees mocks are a good idea, but almost everyone agrees that "mocks used just because everyone else uses mocks" is actively harmful to your codebase. The golden rules of testing are: • a test failure should be as close to a bug report as you can get it to: reading the error message should tell you exactly what is broken and what needs to be fixed • a test should fail for a specific reason (if a function can fail for 9 reasons, it should have 9 tests for failures + at least one test for the case where everything goes well) • most tests should test very specific things, and a lot less tests should be used to test complete scenarios • it is very important that if one thing changes with your software, a very few number of tests fail (those related to what you change). The more tests break when you change things that aren't related to them, the more tests will feel like a chore—on the opposite, if a single test fails per change, you're sure that your change doesn't impact anything else and is thus safe for deploying
👀 1
1
d
I was having another problem. I am not able to configure Gradle as I mostly rely on the project generated by the IDEA. I don't even understand what to configure and why. I was going through the Kotest website and felt like I should be knowing more about Gradle. Can you refer me any resource for this purpose?
https://kotest.io/docs/framework/testing-styles.html And I read this. So, before I write any test, is there any theoretical aspect I have to learn
c
Ahah I had the same problem. Which platforms are you targetting?
d
Are you talking in Gradle context?
c
At the start of your build.gradle file, which Kotlin plugin are you using?
kotlin("jvm")
,
kotlin("js")
,
kotlin("multiplatform")
, etc
Or are you starting a new project from scratch?
In any case, I recommend starting to play with Kotest using one of their example repositories: https://github.com/orgs/kotest/repositories?q=example&type=all&language=&sort= This way, you're sure everything is configured well, and you can take inspiration from the configuration later when you start using it in a real project
👍 1
K 1
d
@CLOVIS
kotlin("jvm")
and
kotlin("multiplatform")
, I think... I don't know detailed difference between the two. But this is what I want to achieve in long term: • At first I want to be capable of developing cli tools (like git, gradle, vim) • Then I want to be capable of developing Desktop Utility apps like PowerToy. • Also I want to do server side programming. (I have experience in Node.js and Express.js kind of frameworks, mainly JavaScript and python background). But I don't have any prior familiarity with
tests
.
My build.gradle file (from a practice project)
Copy code
/*
 * This file was generated by the Gradle 'init' task.
 *
 * This generated file contains a sample Kotlin application project to get you started.
 * For more details take a look at the 'Building Java & JVM projects' chapter in the Gradle
 * User Manual available at <https://docs.gradle.org/8.0/userguide/building_java_projects.html>
 */

plugins {
    // Apply the org.jetbrains.kotlin.jvm Plugin to add support for Kotlin.
    id("org.jetbrains.kotlin.jvm") version "1.8.10"

    // Apply the application plugin to add support for building a CLI application in Java.
    application
}

repositories {
    // Use Maven Central for resolving dependencies.
    mavenCentral()
}

dependencies {
    // Use the Kotlin JUnit 5 integration.
    testImplementation("org.jetbrains.kotlin:kotlin-test-junit5")

    // Use the JUnit 5 integration.
    testImplementation("org.junit.jupiter:junit-jupiter-engine:5.9.1")

    // This dependency is used by the application.
    implementation("com.google.guava:guava:31.1-jre")

    implementation("com.github.ajalt.clikt:clikt:3.5.1")
    testImplementation("org.junit.jupiter:junit-jupiter:5.8.1")
}

application {
    // Define the main class for the application.

    mainClass.set("taskmanner.AppKt")
}

tasks.named<Test>("test") {
    // Use JUnit Platform for unit tests.
    useJUnitPlatform()
}
this is generated by
gradle init
.
c
First, you can't add multiple
kotlin()
plugins in a single Gradle project (
build.gradle.kts
). You should choose one. • JVM creates a Java library or application, meaning it will need the user to install Java to run. It's the easiest to get started because it's the oldest (so they had the time to fix most issues with it). For CLI tools, it's not great because the JVM is a bit slow to start (~0.3–0.5 seconds depending on your machine), but it's still by far the best way to make your first attempt • JS is for websites or other JS scripts. You need a JavaScript executor to run it (a web browser or NodeJS). I guess you could create a NodeJS script like this, but I do not recommend it if you're not very familiar with NodeJS already. Plus, Node is slower than the JVM. • Native is for native apps or libraries, that do not need anything else to run on your machine. It's the youngest platform by far, and the hardest to use. However, it's the best platform for CLI apps, so I recommend you make your first one or two on top of the JVM to understand how to do it, and then try using Native. • Multiplatform is... all of them! It's the plugin to use when you want to create a single app that runs on multiple platforms. It's harder to configure so I don't recommend starting with it, but it's by far the most interesting in my opinion
Yep, your setup looks good for a JVM project. I recommend you use it to get familiar with Kotlin, so you can attempt something else later. If you have trouble with Kotest specifically, you can ask in #kotest
👍 1
d
Is there anything to explore in the gradle setup? (from a customization prospective)
h
I usually work with spring boot and I generate my gradle projects from start.spring.io website. You can start building and running without much knowledge of gradle.
m
If you use IntelliJ IDEA Ultimate you can do this from the IDE (the Spring support is integrated)