找不到Kotlin测试,但有时只有

发布于 2025-02-06 08:14:52 字数 1248 浏览 1 评论 0原文

这是我想运行的测试:

@Test
fun testRequestFails() = runBlocking {
    assertFailsWith(ClientRequestException::class) {
        val result = httpClient.get<MyResponse>("$baseUrl/some/req/that/should/fail") {
            header(API_KEY_HEADER, testApiKey)
        }
    }
}

httpclient.get暂停FUN。当我尝试使用./ gradlew Test或IDEA中运行此操作时,测试被默默跳过!当我尝试在想法中专门运行这个问题时,它说“没有找到给定的测试。”

然而!我可以通过将assertfailswith的结果分配给变量,或者通过将assertequals(1,1)添加到测试结束时,可以找到它。

我可能在Coroutines上做错了什么,但是在我的脑海中,如果我写了一个错误的测试,那应该是 fail ,而不是 消失

它是深层的令人不安的是,测试可能会消失而不会警告!

  1. 是否有办法确保可以发现测试,而无需进行无用的变量分配或无用的断言?
  2. 如果测试未能发现,有没有办法使巨大的警钟消失?

build.gradle.kts的摘要:

tasks.test {
    useJUnitPlatform()
    testLogging {
        events("passed", "skipped", "failed")
    }
}
dependencies {
    testImplementation(kotlin("test-junit5"))
    testImplementation("org.junit.jupiter:junit-jupiter:5.7.0")

    testImplementation("io.ktor:ktor-client-mock-jvm:$ktorVersion")
    testImplementation("io.ktor:ktor-server-tests:$ktorVersion")
}

Here is the test that I'd like to run:

@Test
fun testRequestFails() = runBlocking {
    assertFailsWith(ClientRequestException::class) {
        val result = httpClient.get<MyResponse>("$baseUrl/some/req/that/should/fail") {
            header(API_KEY_HEADER, testApiKey)
        }
    }
}

httpClient.get is a suspend fun. When I try to run this with ./gradlew test or IDEA, the test is silently skipped! When I try to run this one specifically in IDEA, it says "No tests found for given includes."

However! I can make it findable and runnable by either assigning the result of assertFailsWith to a variable, or by adding assertEquals(1,1) to the end of the test.

I'm probably doing something wrong with coroutines, but in my head, if I write a test wrong, it should fail, not disappear

It's deeply troubling that a test can disappear without warning!

  1. Is there a way to ensure the test is discoverable, without doing a useless variable assignment or a useless assert?
  2. Is there a way to make giant alarm bells go off if a test fails to get discovered?

Snippets from build.gradle.kts:

tasks.test {
    useJUnitPlatform()
    testLogging {
        events("passed", "skipped", "failed")
    }
}
dependencies {
    testImplementation(kotlin("test-junit5"))
    testImplementation("org.junit.jupiter:junit-jupiter:5.7.0")

    testImplementation("io.ktor:ktor-client-mock-jvm:$ktorVersion")
    testImplementation("io.ktor:ktor-server-tests:$ktorVersion")
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文