标签: kotlintest

如何在 kotlintest 中编写 beforeEach 和 beforeClass

给出的是来自kotlin-test github docs的示例,但我在这里没有看到 beforeEach 或 beforeClass 概念。我想明白,

  • 如何在每次测试之前执行一次代码/方法
  • 如何在每个测试类之前执行一次代码/方法
class MyTests : StringSpec({
  "length should return size of string" {
    "hello".length shouldBe 5
  }
  "startsWith should test for a prefix" {
    "world" should startWith("wor")
  }
})
Run Code Online (Sandbox Code Playgroud)

kotlin kotlintest

4
推荐指数
2
解决办法
4863
查看次数

kotlin-test:如何测试特定类型,例如:“是 X 的 y 实例”

如何测试 val/var 是否属于预期类型?

我在 Kotlin 测试中缺少什么,例如:

value shouldBe instanceOf<ExpectedType>()
Run Code Online (Sandbox Code Playgroud)

这是我如何实施它:

inline fun <reified T> instanceOf(): Matcher<Any> {
    return object : Matcher<Any> {
        override fun test(value: Any) =
                Result(value is T, "Expected an instance of type: ${T::class} \n Got: ${value::class}", "")

    }
}
Run Code Online (Sandbox Code Playgroud)

instanceof kotlin kotlintest

4
推荐指数
1
解决办法
2887
查看次数

是否有更简单的方法来使用 Kotest 基于属性的测试来测试所有排列?

我正在使用 kotlin + Kotest 属性测试,并尝试使用列表生成器测试 2 个参数的所有排列,如下所示:

"Some test"{
        forAll(4 ,
                Exhaustive.collection(listOf(
                        "a",
                        "b")),
                Exhaustive.collection(listOf(
                        "1",
                        "2"))
        { begins_with, contains ->
            println("$begins_with:$contains")
            ... some validation code...
        }
Run Code Online (Sandbox Code Playgroud)

我希望使用穷举生成器能够以这样的方式生成它们:通过 4 次迭代,所有可能的排列都将被覆盖。像这样:

a:1
b:2
a:2
b:1
Run Code Online (Sandbox Code Playgroud)

相反,详尽的生成器总是按照列出的顺序排列,如下所示:

a:1
b:2
a:1
b:2
Run Code Online (Sandbox Code Playgroud)

这意味着我正在多次测试同一个案例。

我尝试过将一些生成器切换到 Arb,这确实会切换顺序,但不是最佳状态。为了增加命中所有情况的可能性,我必须进行比使用正确顺序时更多的测试。

我也考虑过像这样多次列出相同的元素

"Some test"{
        forAll(4 ,
                Exhaustive.collection(listOf(
                        "a",
                        "b")),
                Exhaustive.collection(listOf(
                        "1",
                        "1",
                        "2",
                        "2"))
        { begins_with, contains ->
            println("$begins_with:$contains")
            ... some validation code...
        }
Run Code Online (Sandbox Code Playgroud)

但这似乎不可持续,尤其是当我想稍后添加更多参数或值时。

有没有办法生成详尽的排列,而不是继续循环每个列表?

kotlin property-based-testing kotlintest kotest

4
推荐指数
1
解决办法
1463
查看次数

如何使kotlintest与Spring一起使用?

我正在尝试将kotlintest与Spring一起使用(不是Spring Boot,只是标准的spring-test)。我发现很难做到。关于我在做什么错的任何指示?我也是Kotlin的新手,所以我很可能做得不好。

到目前为止,这是我尝试过的:

import io.kotlintest.matchers.shouldBe
import io.kotlintest.specs.BehaviorSpec

import org.junit.ClassRule
import org.junit.Rule

import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.test.context.ContextConfiguration
import org.springframework.test.context.junit4.rules.SpringClassRule
import org.springframework.test.context.junit4.rules.SpringMethodRule

open class SomeBean {
  open fun sayHello() = "Hello"
}

@Configuration
open class TestConfig {
  @Bean
  open fun someBean(): SomeBean = SomeBean()
}

@ContextConfiguration(classes = arrayOf(TestConfig::class))
open class MyTests(var someBean: SomeBean) : BehaviorSpec() {
  @Rule
  @JvmField
  val springMethodRule: SpringMethodRule = SpringMethodRule()

  companion object {
    @ClassRule
    @JvmField
    val SPRING_CLASS_RULE: SpringClassRule = SpringClassRule()
  }

  init {
    given("A test") {
      `when`("When my …
Run Code Online (Sandbox Code Playgroud)

spring-test kotlin kotlintest

2
推荐指数
2
解决办法
2405
查看次数

kotlintest如何测试Spring Boot应用程序

Spring Boot应用程序的集成测试始终首先启动Web服务器。

Spring Boot测试的最简单测试如下所示,如何使用kotlintest迁移它?

@ExtendWith(SpringExtension::class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class ReportApplicationTests {

    @Test
    fun `Server can be launched`() {
    }

}
Run Code Online (Sandbox Code Playgroud)

kotlin spring-boot kotlintest

1
推荐指数
1
解决办法
1188
查看次数