当 运行 多个同时测试时测试失败
The test failed when running multiple tests at the same time
我编写了测试片段的代码。编写了测试方法 testChangeSystemsMeasuresToMetric、testChangeSystemsMeasuresToImperial、checkFragmentOpen,但如果将它们 运行 放在一起,testChangeSystemsMeasuresToImperial 将失败,并显示以下 androidx.test.espresso.AmbiguousViewMatcherException: 'is assignableible class class error.Problem views are marked 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())
}
}
我能够使用 orchestrator 解决我的问题
build.gradle
dependencies{
androidTestUtil 'androidx.test:orchestrator:1.2.0'
}
android{
testOptions {
execution 'ANDROIDX_TEST_ORCHESTRATOR'
}
}
是的,我还是不明白问题出在哪里,如果你能告诉我为什么会出现这个问题,我将不胜感激。
我编写了测试片段的代码。编写了测试方法 testChangeSystemsMeasuresToMetric、testChangeSystemsMeasuresToImperial、checkFragmentOpen,但如果将它们 运行 放在一起,testChangeSystemsMeasuresToImperial 将失败,并显示以下 androidx.test.espresso.AmbiguousViewMatcherException: 'is assignableible class class error.Problem views are marked 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())
}
}
我能够使用 orchestrator 解决我的问题
build.gradle
dependencies{
androidTestUtil 'androidx.test:orchestrator:1.2.0'
}
android{
testOptions {
execution 'ANDROIDX_TEST_ORCHESTRATOR'
}
}
是的,我还是不明白问题出在哪里,如果你能告诉我为什么会出现这个问题,我将不胜感激。