android_local_test

Android local test target.

Description#

Compiles Android Java and Kotlin test sources, then runs them on the host Java virtual machine through Struo's generic test capability. This target is for local unit tests, not device or emulator instrumentation tests.

The runner scans compiled test classes and invokes zero-argument methods whose name starts with test, or methods annotated with org.junit.Test or kotlin.test.Test. Assertion libraries such as JUnit can be added through classpath and runtime_classpath.

Static nested test classes are discovered as well as top-level classes. Public inherited test methods run on the concrete suite, with overridden methods selected once. Anonymous classes, local classes, abstract classes, and non-static inner classes are not independent test suites. Methods or suites annotated with org.junit.Ignore or kotlin.test.Ignore are reported as skipped without invoking them or initializing ignored suites. The reflection runner does not implement framework lifecycle hooks or custom runners such as Robolectric.

Set test_framework = "junit4" to delegate execution to native JUnit 4, including @Before/@After, rules, expected exceptions, assumptions, and @RunWith runners such as Robolectric. Supply JUnit, the custom runner and all its runtime dependencies through declared classpaths or dependency providers. Framework configuration and SDK jars must be declared inputs too; Struo does not infer Gradle configuration or fingerprint files downloaded during a test. For Robolectric, prepare its SDK jars before testing, declare them through runtime_classpath, and use offline resolution through jvm_flags rather than letting the runner download undeclared dependencies. This mode reports native initialization failures instead of silently skipping malformed annotated tests. Android resource tests also need the linked test resource package, its matching runtime R classes, and the runner configuration as declared inputs. A static library's unassigned resource IDs are not a substitute for linked runtime IDs.

Coverage-assisted Selection#

Host-side tests support the same isolated JaCoCo collector as kotlin_jvm_test. Set coverage_per_test = true, declare product coverage_sources, and provide matching package-relative jacoco_agent and jacoco_cli JARs. The initial collector requires Linux, Python 3 and strace. Struo discovers current units, measures each exact invocation, and preserves zero-hit class-file sources. Subprocesses, extra reported cases, missing instrumentation and unsuccessful observations cannot establish complete checkpoints. Coverage measurements bypass execution caching. This host collector does not collect device instrumentation coverage.

Attributes#

AttributeTypeRequiredDefaultDescription
test_frameworkstringnoreflectionreflection for convention-based methods or junit4 for native lifecycle and runner dispatch
coverage_per_testboolnofalseMeasure each exact host execution unit with JaCoCo
coverage_sourceslist<string>no[]Product Java or Kotlin source globs required in the class-file universe
jacoco_agentstringnoemptyPackage-relative JaCoCo runtime agent JAR
jacoco_clistringnoemptyPackage-relative matching JaCoCo command-line JAR
compile_sdkintnohighest installedAndroid Software Development Kit platform level used for android.jar
build_tools_versionstringnohighest installedAndroid Software Development Kit build-tools version
android_sdkstringnoenvAndroid Software Development Kit root, otherwise ANDROID_HOME or ANDROID_SDK_ROOT
java_language_levelstringno17Java source and target level passed to javac
javac_optslist<string>no[]Additional javac flags
javacoptslist<string>no[]Bazel-compatible alias for additional javac flags
kotlinc_optslist<string>no[]Additional kotlinc flags
classpathlist<string>no[]Additional Java archive files used while compiling and running tests
runtime_classpathlist<string>no[]Additional Java archive files used only while running tests
argslist<string>no[]Additional fully qualified class or Class#method filters passed to the local test runner
jvm_flagslist<string>no[]Additional flags passed to the host Java virtual machine before the test classpath
test_classstringnoemptyOptional fully qualified test class or Class#method filter
envmap<string,string>no{}Bazel-compatible environment variables passed to the local test runner before test_env overrides
env_inheritlist<string>no[]Host environment variable names inherited by the local test runner before explicit test environment values
test_envmap<string,string>no{}Environment variables passed to the local test runner
labelslist<string>no[]Labels exposed through struo_test_info for test discovery
timeout_msintnoOptional test timeout in milliseconds

Accepted but unsupported attributes: custom_package, densities, enable_data_binding, manifest, manifest_values, nocompress_extensions, plugins, resource_configuration_filters, resource_jars, resource_strip_prefix, runtime_deps, and stamp. Non-empty values under [target.attrs] fail validation. Use the runtime_deps dependency role under [target.dependencies] for runtime-only libraries.

Tool override attrs are also available for javac, java, java_home, kotlinc, kotlin_home, kotlin_stdlib, and aapt2.

Dependency Edges#

EdgeAcceptsDescription
depsandroid_library, java_libraryLibraries under test. android_library targets also emit the java_library provider.
runtime_depsandroid_library, java_libraryLibraries added only to the host test runtime classpath

Providers#

The target emits android_local_test and struo_test_info.

Capabilities#

CapabilityOutput groups
buildclasses
testdefault, test_results, logs

Outputs#

OutputLocation
Test results.struo/out/<target>/test/test_results.json
Test log.struo/out/<target>/test/android-local-test.log
Native runner output.struo/out/<target>/test/native_results.txt

Limitations#

android_local_test runs on the host Java virtual machine. It does not install an application package, start an Android device, or run instrumentation tests. Tests that depend on Android framework behavior usually need a local framework adapter such as Robolectric or android_instrumentation_test.

Example#

toml
[[target]] name = "Greeting" kind = "android_library" srcs = ["src/main/**/*.kt"] [target.attrs] namespace = "dev.struo.greeting" manifest = "AndroidManifest.xml" resource_files = [] [[target]] name = "GreetingTests" kind = "android_local_test" srcs = ["src/test/**/*.kt"] deps = ["./Greeting"] [target.dependencies] runtime_deps = ["./TestRuntime"] [target.attrs] labels = ["unit"]