Kotest 프로젝트 설정(Setup)

Kotest는 Kotlin으로 작성된 강력한 테스트 프레임워크이다. 프로젝트 설정을 하는 방법에 대해서 설명한다.

의존성 추가

Kotest를 사용하기 위해서는 먼저 프로젝트에 라이브러리를 추가해야 한다. Kotest 테스트 프레임워크는 JVM, Javascript 및 Native에서 지원된다. 여러 플랫폼에서 Kotest를 사용하려면 아래 설명된 대로 개별 플랫폼에 맞게 대한 단계를 설정하면 된다.

JVM/Gradle

JVM/Gradle 프로젝트에서의 Kotest는 JUnit Platform gradle 플러그인을 사용한다. Gradle 4.6 이상에서는 테스트 유형이 Test인 작업 내에 useJUnitPlatform()을 추가하고 Kotest junit5 런처 종속성을 추가하기만 하면 된다.

Gradle + Groovy(빌드 파일: build.gradle)를 사용하는 경우:

test {
  useJUnitPlatform()
}

또는 Gradle + Kotlin(빌드 파일: build.gradle.kts)을 사용하는 경우:

tasks.withType<Test>().configureEach {
   useJUnitPlatform()
}

그러고 다음과 같이 Kotest 의존성을 추가한다:

repositories {
    mavenCentral()
}

dependencies {
    testImplementation("io.kotest:kotest-runner-junit5:${kotestVersion}")
    // 필요한 경우 다른 모듈에 대한 종속성도 추가한다.
}

JVM/Maven

JVM/Maven 프로젝트의 경우에는 빌드 파일(pom.xml)에 다음과 같이 구성이 필요하다.

junit 테스트를 위해 surefire 플러그인을 구성해야 한다.

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-surefire-plugin</artifactId>
   <version>2.22.2</version>
</plugin>

그런 다음 종속성을 Kotest JUnit5 런너를 추가한다.

<dependencies>
    <dependency>
        <groupId>io.kotest</groupId>
        <artifactId>kotest-runner-junit5</artifactId>
        <version>{kotestVersion}</version>
        <scope>test</scope>
    </dependency>
</dependencies>

Kotlin/JS

예를 들어,

plugins {
  id("io.kotest.multiplatform") version "5.0.2"
}

엔진 종속성을 commonTest 종속성 블록에 추가한다:

kotlin {
  targets {
    js(IR) { // LEGACY or BOTH are unsupported
      browser() // to compile for the web
      nodejs() // to compile against node
    }
  }

  sourceSets {
    val commonTest by getting {
      dependencies {
        implementation("io.kotest:kotest-framework-engine:$version")
      }
    }
  }
}

FunSpec, ShouldSpec, StringSpec을 사용하여 테스트를 작성할 수 있다. 테스트는 commonTest 또는 jsTest 소스 집합에 배치할 수 있다. gradle check 명령을 사용하여 테스트를 실행한다.

자바스크립트 테스트 엔진은 JVM 테스트 엔진과 비교할 때 기능이 제한되어 있다. 가장 큰 제한 사항은 Kotlin이 런타임에 어노테이션을 자바스크립트 코드에 노출하지 않으므로 어노테이션 기반 구성이 작동하지 않는다는 것이다.

Kotlin/Native

Kotlin/Navive 프로젝트에서도 Kotlin/JS 프로젝트와 동일하게 Kotest 멀티플랫폼 Gradle 플러그인을 추가해야 한다.

plugins {
  id("io.kotest.multiplatform") version "5.0.2"
}

엔진 종속성을 commonTest 종속성 블록에 추가한다:

kotlin {
  targets {
    linuxX64() // can add any supported native targets such as linux, mac, windows etc
  }
}
sourceSets {
  val commonTest by getting {
    dependencies {
      implementation("io.kotest:kotest-framework-engine:$version")
    }
  }
}

테스트는 commonTest 또는 특정 네이티브 소스 집합에 배치할 수 있다. gradle check 명령을 사용하여 테스트를 실행한다.

네이티브 테스트 엔진은 JVM 테스트 엔진과 비교할 때 기능이 제한적이다. 가장 큰 제한 사항은 Kotlin이 런타임에 어노테이션을 네이티브 코드에 노출하지 않으므로 어노테이션 기반 구성이 작동하지 않는다는 것이다.

참고 IntelliJ Kotest 플러그인은 IDE에서 일반, 네이티브 또는 JS 테스트를 실행하는 것을 지원하지 않는다. Gradle 검사 작업을 사용해야 한다.

Android

Android의 Kotest는 JUnit Platform gradle 플러그인을 사용한다. 이를 위해서는 빌드 파일에서 안드로이드 테스트 옵션 블록을 구성한 다음 Kotest junit5 런처 종속성을 추가해야 한다.

android.testOptions {
   unitTests.all {
      it.useJUnitPlatform()
   }
}
dependencies {
   testImplementation 'io.kotest:kotest-runner-junit5:{version}'
}

kotest 버전

여기서 ${kotestVersion}을 사용하고자 하는 Kotest 버전을 지정한다.

2024년 4월 기준으로 kotest 최신 버전은 2024년 3월 11일에 출시된 5.8.1이다. https://central.sonatype.com/artifact/io.kotest/kotest-runner-junit5

IntelliJ IDEA 에서 Kotest 환경 만들기

IntelliJ IDEA에서 Kotest 구동하려면, Kotest 플러그인을 설치해야 한다.

[Setting]-[Plugins]에서 가서 kotest를 검색해서 설치한다.

Kotest Plugin


참조




최종 수정 : 2024-04-23