【问题标题】:Run Jetpack compose tests with AndroidJunit4 and no device使用 AndroidJunit4 运行 Jetpack 撰写测试,无需设备
【发布时间】:2020-12-30 22:03:28
【问题描述】:

我是 android 测试的新手,不知道这是否会发生。
是否可以使用 androidx.test.ext AndroidJunit4 运行器运行 Jetpack compose 单元测试?

所以我们不需要运行instrumentation 测试和使用设备。

这是我尝试过的:

app/src/test/:

@RunWith(AndroidJUnit4::class)
class ExampleUnitTest {

    @get:Rule
    val activityScenarioRule = ActivityScenarioRule(MainActivity::class.java)

    @get:Rule
    val composeTestRule = createAndroidComposeRule<MainActivity>()
    // createComposeRule() if you don't need access to the activityTestRule

    @Test
    fun greeting_showsGreetingText() {
        // Start the app
        composeTestRule.setContent {
            analyzerTheme {
                // A surface container using the 'background' color from the theme
                Surface(color = MaterialTheme.colors.background) {
                    Greeting("Android")
                }
            }
        }
        composeTestRule.onNodeWithText("Hello Android!").assertIsDisplayed()
    }
}

运行测试我得到以下错误:

No tests found for given includes: [greeting_showsGreetingText]

【问题讨论】:

  • 如果你想在单元测试中使用 ActivityScenarioRule,我相信你仍然需要使用 Robolectric。

标签: android android-testing android-jetpack-compose


【解决方案1】:

beta09 开始,撰写测试应该现在能够与 Robolectric 一起使用,但存在一些限制。 参考Compose release notes

诚然,我无法让它工作,但我还没有很努力(还):)

【讨论】:

    【解决方案2】:

    Compose 测试仍然是仪器测试,没有物理设备或模拟器就无法运行

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-12
      • 1970-01-01
      • 2020-11-03
      • 2020-03-18
      • 2022-06-25
      • 1970-01-01
      • 2020-08-21
      • 1970-01-01
      相关资源
      最近更新 更多