Skip to content

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 Once'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.

Attributes

AttributeTypeRequiredDefaultDescription
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 once_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 fail validation.

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.

Providers

The target emits android_local_test and once_test_info.

Capabilities

CapabilityOutput groups
buildclasses
testdefault, test_results, logs

Outputs

OutputLocation
Test results.once/out/<target>/test/test_results.json
Test log.once/out/<target>/test/android-local-test.log
Native runner output.once/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.once.greeting"
manifest = "AndroidManifest.xml"
resource_files = []

[[target]]
name = "GreetingTests"
kind = "android_local_test"
srcs = ["src/test/**/*.kt"]
deps = ["./Greeting"]

[target.attrs]
labels = ["unit"]

Released under an open-source license.