Destroyer
Destroyer

Reputation: 795

The test failed when running multiple tests at the same time

I wrote the code for testing the fragment. Wrote test methods testChangeSystemsMeasuresToMetric, testChangeSystemsMeasuresToImperial, checkFragmentOpen, but if you run them together, testChangeSystemsMeasuresToImperial fails with the androidx.test.espresso.AmbiguousViewMatcherException: 'is assignableible class class error.Problem views are marked with '**** MATCHES ****' below. At the same time, each tete completes successfully and if you run testChangeSystemsMeasuresToImperial and testChangeSystemsMeasuresToMetric together, then all tests will run successfully. What could be the problem?

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())
    }

}

Upvotes: 2

Views: 351

Answers (1)

Destroyer
Destroyer

Reputation: 795

I was able to solve my problem with orchestrator

build.gradle

dependencies{
    androidTestUtil 'androidx.test:orchestrator:1.2.0'
}

android{
    testOptions {
        execution 'ANDROIDX_TEST_ORCHESTRATOR'
    }
}

True, I still do not understand what the problem was, I will be grateful if you tell me why this problem arose.

Upvotes: 2

Related Questions