我写了测试片段的代码。编写了测试方法testChangeSystemsMeasuresToMetric,testChangeSystemsMeasuresToImperial,checkFragmentOpen,但如果同时运行它们,则testChangeSystemsMeasuresToImperial会失败,并出现androidx.test.espresso.AmbiguousViewMatcherException:'是可分配的类类错误。问题视图标记为'**** MATCHES **** '在下面。 同时,每个测试都成功完成,并且如果您同时运行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())
}
}
答案 0 :(得分:0)
我能够用Orchestrator解决我的问题
build.gradle
dependencies{
androidTestUtil 'androidx.test:orchestrator:1.2.0'
}
android{
testOptions {
execution 'ANDROIDX_TEST_ORCHESTRATOR'
}
}
是的,我仍然不明白问题出在什么地方,如果您告诉我为什么会出现此问题,我将不胜感激。