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?
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)
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()
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With