# Selectively running Android UI tests

DevFeed: [Selectively running Android UI tests](<https://devfeed.tech/articles/selectively-running-android-ui-tests-25843.md>)

Original publisher: [Read original article](<https://segunfamisa.com/posts/selectively-run-android-ui-tests>)

Author: Segun Famisa

Published: 2024-12-14T00:00:00Z

Content type: tutorial

Language: en

Sources: [Segun Famisa](<https://devfeed.tech/sources/segun-famisa.md>)

Topics: [Android](<https://devfeed.tech/topics/android.md>), [Instrumentation](<https://devfeed.tech/topics/instrumentation.md>), [Gradle](<https://devfeed.tech/topics/gradle.md>), [Command-line interface](<https://devfeed.tech/topics/cli.md>)

Tags: [adb](<https://devfeed.tech/tags/adb.md>), [android](<https://devfeed.tech/tags/android.md>), [ci](<https://devfeed.tech/tags/ci.md>), [flag](<https://devfeed.tech/tags/flag.md>), [gradle](<https://devfeed.tech/tags/gradle.md>), [instrumentation](<https://devfeed.tech/tags/instrumentation.md>), [junit](<https://devfeed.tech/tags/junit.md>), [property](<https://devfeed.tech/tags/property.md>), [pull-request](<https://devfeed.tech/tags/pull-request.md>), [test-lab](<https://devfeed.tech/tags/test-lab.md>), [tests](<https://devfeed.tech/tags/tests.md>)

## AI overview

This tutorial explains how to selectively run additional checks in Android UI tests. It uses a custom annotation and AndroidJUnitRunner filters to target specific tests, then passes arguments through adb or Gradle so the checks can be enabled only under chosen conditions, such as CI runs.

## Source excerpt

Intro This is really a note for my future self. Recently, I had a situation where I wanted to perform some extra checks after a test has been run. These additional checks are done after each test is run via a Junit rule SpecialRule which is applied to a test suite. For the purpose of illustration, let's say our SpecialRule looks like this: class SpecialRule : TestRule { override fun apply(statement: Statement, desc: Description): Statement { return object : Statement() { override fun evaluate() { try { // Run the test. statement.evaluate() } finally { // Perform extra checks after the test. Log.d("SpecialRule", "performing extra checks") } } } } } However, we were concerned about the run duration of the tests being extended significantly by the extra checks, so we wanted a bit more control. That led to some constraints. We wanted to: be able to selectively perform those checks on specific tests perform those checks only under certain conditions. Constraint #1 - Selectively perform additional checks on specific tests The immediate approach that came to mind was to use a custom annotation - @SpecialTest, and then somehow find a way to target the tests that are annotated with this. Luckily, the AndroidJunitRunner provides various filters to target specific annotations. For example, to run all the tests annotated with @SpecialTest , we will use something like: adb shell am instrument -w -e annotation com.mypackage.SpecialTest com.android.foo/androidx.test.runner.AndroidJUnitRunner If you use Flank to run your tests on Firebase Test Lab, then you can use: --test-targets: "annotation com.mypackage.SpecialTest" Now that we can selectively target the tests, we need to extend our test rule to only perform these checks if the test is annotated with @SpecialTest annotation. Our SpecialRule then becomes something like: class SpecialRule : TestRule { override fun apply(statement: Statement, desc: Description): Statement { return object : Statement() { override fun evaluate() { t