[Spring Boot] Kotlin + Kotest / Continually 로 일정 시간 동안 반복적인 테스트 진행하기



kotest 에서 정해진 시간 내에 지속적으로 테스트를 수행하고 싶을 때가 있을 때 어떻게 하면 반복적으로 테스트를 진행할 수 있을까요? 시간을 정해놓고 반복 테스트를 진행하려면 continually 함수를 사용하면 손쉽게 테스트를 반복적으로 일정 시간 내에 진행할 수 있습니다.

suspend fun <T> continually(duration: Duration, poll: Duration, f: suspend () -> T) =
   continually(duration, poll.fixed(), f = f)

suspend fun <T> continually(
   duration: Duration,
   interval: Interval = 10.milliseconds.fixed(),
   f: suspend () -> T
) = Continually<T>(duration, interval).invoke(f)

continually 는 위와 같은 구조를 가지고 있는데요. duration 에 값을 넣어주면 해당 시간 동안 테스트를 수행합니다. 혹은, interval 에 값을 넣어주어 테스트 별 delay를 얼마나 줄지 설정할 수 있습니다.

class MyTests : ShouldSpec() {
  init {
    should("pass for 60 seconds") {
      continually(60.seconds) {
        // code here that should succeed and continue to succeed for 60 seconds
      }
    }
  }
}
class MyTests: ShouldSpec() {
  init {
    should("pass for 60 seconds") {
      continually(60.seconds, 5.seconds) {
        // code here that should succeed and continue to succeed for 60 seconds
      }
    }
  }
}

일정 시간을 정해놓고 테스트를 수행하면 아래와 같이 지정된 시간에 테스트를 반복적으로 수행합니다. 테스트를 반복적으로 진행하는 것이 필요한 경우 유용하게 쓸 수 있는 방법입니다.

image


reference

  • https://kotest.io/docs/assertions/continually.html