是否有针对JetBrains @Composable功能的真正乘法测试设置?

发布于 2025-01-26 23:51:30 字数 1182 浏览 6 评论 0原文

我希望能够在我的公共项目的androidTest目标中获得@composable上下文,以便测试驻留在CommanMain ,例如ContentLocalProviders和Beatouts。类似:

@Test fun testSomethingComposable() = runComposeTest {
    @Composable fun <M> buildMutableState(model: M) { /* ... */ }

    assertNotNull { buildMutableState(initialState).value }
}

我以的期望期望fun runco​​mposetest(content:@composable() - &gt; unit) commontest ,但只有jvmtest版本有效。 createComposerule()。setContent {} androidx提供的功能仅在仪器测试中起作用。

还有其他方法吗?同时,我刚刚将测试推到了jvmtest,以便我可以向前迈进。

jvmtest版本:

import androidx.compose.ui.window.application
actual fun runComposeTest(content: @Composable () -> Unit) = runTest {
    application(false) {content}
}

但是以下内容在androidTest中失败:

actual fun runComposeTest(content: @Composable () -> Unit) = runTest {
    val rule = createComposeRule()
    rule.setContent(content)
}

...出于某种明显的原因。

I'd love to be able to get a @Composable context running in the androidTest target of my common project, in order to test higher-order components that reside in commonMain, such as ContentLocalProviders and layouts. Something like:

@Test fun testSomethingComposable() = runComposeTest {
    @Composable fun <M> buildMutableState(model: M) { /* ... */ }

    assertNotNull { buildMutableState(initialState).value }
}

I began with expect fun runComposeTest(content: @Composable ()->Unit) inside of commonTest, but only the jvmTest version works. The createComposeRule().setContent {} function offered by AndroidX only works within an instrumented test.

Any other way around this? Meanwhile, I've just pushed my tests down to jvmTest so that I can move forward.

The jvmTest version:

import androidx.compose.ui.window.application
actual fun runComposeTest(content: @Composable () -> Unit) = runTest {
    application(false) {content}
}

But the following fails in androidTest:

actual fun runComposeTest(content: @Composable () -> Unit) = runTest {
    val rule = createComposeRule()
    rule.setContent(content)
}

... For somewhat obvious reasons.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

表情可笑 2025-02-02 23:51:30

事实证明,他们有一个测试框架,据我所知,文档中没有提及。我添加了a nofollow noreferrer“

但基本上,您希望将org.jetbrains.com.pose.ui:ui-test-junit4添加到您的项目中。我正在使用Alpha软件运行:这就是:

kotlin { sourceSets {
  val commonTest {
    dependencies {
      /* ... */
      implementation(kotlin("test-junit"))
      implementation(kotlin("test-common"))
      implementation("org.jetbrains.compose.ui:ui-test-junit4:1.2.0-alpha01-dev620")
    }
  }
  val jvmMain by getting {
    dependencies {
      /**
       * Note this is needed for tests to run.
       **/
      implementation(compose.desktop.currentOs)
    }
  }
  val jvmTest by getting {
    dependencies {
      implementation("junit:junit:4.13.2")
    }
  }
}

这些测试当前在Junit5下不起作用,或者我在某个地方阅读(抱歉,没有参考)。因此,请注意显式kotlin(“ test-junit”)
而不是“ test”,它将加载junit5。

Turns out they have a test framework that to my knowledge hasn't been announced and receives no mention in the documentation. I added a gist with the full build.gradle.kts.

But basically, you're looking to add org.jetbrains.compose.ui:ui-test-junit4 to your project. I'm running on alpha software so that's:

kotlin { sourceSets {
  val commonTest {
    dependencies {
      /* ... */
      implementation(kotlin("test-junit"))
      implementation(kotlin("test-common"))
      implementation("org.jetbrains.compose.ui:ui-test-junit4:1.2.0-alpha01-dev620")
    }
  }
  val jvmMain by getting {
    dependencies {
      /**
       * Note this is needed for tests to run.
       **/
      implementation(compose.desktop.currentOs)
    }
  }
  val jvmTest by getting {
    dependencies {
      implementation("junit:junit:4.13.2")
    }
  }
}

These tests currently don't work under JUnit5, or so I read somewhere (sorry no reference). Therefore note the explicit kotlin("test-junit")
instead of "test", which will load junit5.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文