【问题标题】:The test failed when running multiple tests at the same time同时运行多个测试时测试失败
【发布时间】:2020-05-21 14:24:56
【问题描述】:

我编写了用于测试片段的代码。编写了测试方法 testChangeSystemsMeasuresToMetric、testChangeSystemsMeasuresToImperial、checkFragmentOpen,但是如果一起运行它们,testChangeSystemsMeasuresToImperial 会失败,并出现 androidx.test.espresso.AmbiguousViewMatcherException: 'is assignable class class error.Problem views are marking with '**** MATCHES **** ' 以下。 同时,每个 tete 都成功完成,如果同时运行 testChangeSystemsMeasuresToImperial 和 testChangeSystemsMeasuresToMetric,那么所有测试都会成功运行。可能是什么问题?

class ProfileFragmentTest {
    @Rule
    @JvmField
    val activityRule = ActivityTestRule(MainActivity::class.java)

    @Rule
    @JvmField
    val dataBindingIdlingResourceRule = DataBindingIdlingResourceRule(activityRule)

    private val testProfile = Profile(SystemsMeasures.Metric, ObservableBoolean(true),
            ObservableInt(0), ObservableDouble(0.0))

    @Before
    fun setUp() {
        CurrentProfile.currentProfile = testProfile

        onView(withId(R.id.drawer_layout))
                .check(matches(isClosed(Gravity.LEFT)))
                .perform(DrawerActions.open());

        onView(withId(R.id.nav_view))
                .perform(NavigationViewActions.navigateTo(R.id.nav_profile))
    }

    @Test
    fun checkFragmentOpen() {
        onView(withId(R.id.profile_fragment)).check(matches(isDisplayed()));
    }

    @Test
    fun testChangeSystemsMeasuresToImperial() {
        val itemText: String = InstrumentationRegistry.getInstrumentation().targetContext.getString(R.string.poundsMassUnit)
        clickChangeSystemsMeasures(itemText)
        assert(testProfile.systemMeasures == SystemsMeasures.Imperial)
    }

    @Test
    fun testChangeSystemsMeasuresToMetric() {
        testProfile.systemMeasures = SystemsMeasures.Imperial
        val itemText: String = InstrumentationRegistry.getInstrumentation().targetContext.getString(R.string.kilogramsMassUnit)
        clickChangeSystemsMeasures(itemText)
        assert(testProfile.systemMeasures == SystemsMeasures.Metric)
    }

     private fun clickChangeSystemsMeasures(itemText: String) {
        val spinnerId: Int = R.id.spinner
        onView(withId(spinnerId)).perform(click())
        onData(allOf(`is`(instanceOf(String::class.java)), `is`(itemText))).perform(click())
    }

}

【问题讨论】:

    标签: android kotlin automated-tests android-espresso


    【解决方案1】:

    我能够使用 Orchestrator 解决我的问题

    build.gradle

    dependencies{
        androidTestUtil 'androidx.test:orchestrator:1.2.0'
    }
    
    android{
        testOptions {
            execution 'ANDROIDX_TEST_ORCHESTRATOR'
        }
    }
    

    确实,我还是不明白问题出在哪里,如果你能告诉我为什么会出现这个问题,我将不胜感激。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-25
      • 2013-04-07
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      • 2017-10-12
      相关资源
      最近更新 更多