给出的是来自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")
  }
})
如何测试 val/var 是否属于预期类型?
我在 Kotlin 测试中缺少什么,例如:
value shouldBe instanceOf<ExpectedType>()
这是我如何实施它:
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}", "")
    }
}
我正在使用 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...
        }
我希望使用穷举生成器能够以这样的方式生成它们:通过 4 次迭代,所有可能的排列都将被覆盖。像这样:
a:1
b:2
a:2
b:1
相反,详尽的生成器总是按照列出的顺序排列,如下所示:
a:1
b:2
a:1
b:2
这意味着我正在多次测试同一个案例。
我尝试过将一些生成器切换到 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...
        }
但这似乎不可持续,尤其是当我想稍后添加更多参数或值时。
有没有办法生成详尽的排列,而不是继续循环每个列表?
我正在尝试将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 …Spring Boot应用程序的集成测试始终首先启动Web服务器。
Spring Boot测试的最简单测试如下所示,如何使用kotlintest迁移它?
@ExtendWith(SpringExtension::class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class ReportApplicationTests {
    @Test
    fun `Server can be launched`() {
    }
}