Created
March 26, 2020 22:32
-
-
Save fkirc/08bd2f0ee6f8fd338a257481e5f618ee to your computer and use it in GitHub Desktop.
Make Android UI tests fail if an error gets logged to Timber
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.util.Log | |
import androidx.test.espresso.Espresso | |
import org.junit.Assert | |
import timber.log.Timber | |
/** | |
* During UI tests, we want to fail the test immediately if any "non-fatal error" happens. | |
* The purpose of non-fatal errors is to inform as about production failures without crashing the entire app. | |
*/ | |
object NonFatalAbortTree: Timber.Tree() { | |
override fun log(priority: Int, tag: String?, message: String, throwable: Throwable?) { | |
if (priority == Log.ERROR || priority == Log.ASSERT) { | |
abortTest(message=message, throwable = throwable) | |
} | |
} | |
private fun abortTest(message: String, throwable: Throwable?) { | |
if (disabled) { | |
return | |
} | |
Assert.fail(message) | |
throw IllegalStateException(message, throwable) | |
} | |
fun plantInTimber() { | |
Timber.plant(this) | |
} | |
private var disabled = false | |
fun runWithoutAborts(waitIdle: Boolean = true, runnable: () -> Unit) { | |
disabled = true | |
runnable() | |
if (waitIdle) { | |
Espresso.onIdle() | |
} | |
disabled = false | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
Call
NonFatalAbortTree.plantInTimber()
somewhere in the setup code of the UI tests.