Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run espresso test multiple times

Sometimes I faced with rare bug in my application. But I can't reproduce it as it's very rare. So, I decided to write simple espresso test:

@RunWith(AndroidJUnit4::class)
@LargeTest
class MainActivityTest {

    val password = "1234"

    @Rule @JvmField
    var mActivityRule: ActivityTestRule<MainActivity> = ActivityTestRule(MainActivity::class.java)

    @Test
    fun checkNotesListNotEmpty() {
        onView(withId(R.id.password_edit_text)).perform(typeText(password))
        onView(withId(R.id.notes_recycler_view)).check { view, noMatchingViewException ->
            if (noMatchingViewException != null) throw noMatchingViewException
            assertThat((view as RecyclerView).adapter.itemCount,  Matchers.`is`(1))
        }
    }
}

How can I loop this test and stop it when matching fails?

like image 760
Alexandr Avatar asked Aug 28 '16 16:08

Alexandr


2 Answers

Solution presented by @mklimek in Kotlin.

How to use:

@Rule @JvmField
var repeatRule: RepeatRule = RepeatRule()

@Test
@RepeatTest(100)
fun checkNotesListNotEmpty() {

RepeatRule.kt

class RepeatRule : TestRule {

    private class RepeatStatement(private val statement: Statement, private val repeat: Int) : Statement() {
        @Throws(Throwable::class)
        override fun evaluate() {
            for (i in 0..repeat - 1) {
                statement.evaluate()
            }
        }
    }

    override fun apply(statement: Statement, description: Description): Statement {
        var result = statement
        val repeat = description.getAnnotation(RepeatTest::class.java)
        if (repeat != null) {
            val times = repeat.value
            result = RepeatStatement(statement, times)
        }
        return result
    }
}

RepeatTest.kt

@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.ANNOTATION_CLASS)
annotation class RepeatTest(val value: Int = 1)
like image 155
Alexandr Avatar answered Oct 24 '22 12:10

Alexandr


Can't you have a separate test which loops that one? (if it is a one-of-its-kind situation):

@RunWith(AndroidJUnit4::class)
@LargeTest
class MainActivityTest {
    ...

    @Test fun checkNotesListNotEmpty() {...}

    @Test fun loopCheckNotesListNotEmpty() {
        while(true)
            checkNotesListNotEmpty()
    }
}
like image 26
voddan Avatar answered Oct 24 '22 10:10

voddan