AOSP Expert & Production Engineering
2 min read

TradeFed Test Harness

TradeFed Architecture

Trade Federation (TradeFed or TF) is the continuous test framework used extensively throughout AOSP. It is not a test runner like JUnit; rather, it is a robust harness that manages the execution of tests across multiple physical or virtual devices.

TradeFed handles the heavy lifting of:

  • Device discovery and allocation via adb.
  • Environment setup (flashing images, installing APKs, pushing configuration files).
  • Test execution dispatching to specific runners (e.g., AndroidJUnitRunner, native gtest).
  • Result collection, log aggregation, and reporting.

Test Runner, Result Reporters, Log Savers

A TradeFed execution pipeline is built from pluggable Java components:

  1. TargetPreparer: Prepares the device. For example, PushFilePreparer pushes a native binary to /data/local/tmp.
  2. TestRunner: Executes the test. InstrumentationTest drives JUnit, while GTest drives native C++ tests.
  3. ResultReporter: Formats the output. Reporters can print to the console, generate XML files, or upload results directly to a database or dashboard.
  4. LogSaver: Automatically collects logcat, dmesg, and bug reports when a test fails, ensuring developers have the context needed to debug.

TradeFed XML Config Files

TradeFed is heavily configuration-driven using XML. To run a custom test, you define its pipeline in an AndroidTest.xml file placed alongside your Android.bp.

<?xml version="1.0" encoding="utf-8"?>
<configuration description="Config for MyFrameworkTests">
    <!-- Ensure the device screen is on -->
    <target_preparer class="com.android.tradefed.targetprep.DeviceSetup">
        <option name="screen-always-on" value="on" />
    </target_preparer>

    <!-- Install the test APK -->
    <target_preparer class="com.android.tradefed.targetprep.suite.SuiteApkInstaller">
        <option name="cleanup-apks" value="true" />
        <option name="test-file-name" value="MyFrameworkTests.apk" />
    </target_preparer>

    <!-- Run the instrumentation -->
    <test class="com.android.tradefed.testtype.AndroidJUnitTest" >
        <option name="package" value="com.android.myframework.tests" />
        <option name="runner" value="androidx.test.runner.AndroidJUnitRunner" />
    </test>
</configuration>

When you run atest MyFrameworkTests, atest parses this XML and passes it to TradeFed to execute the pipeline.

Running Custom Test Suites with TradeFed

You can build entirely custom suites by composing multiple TradeFed configurations. You can invoke TradeFed directly via its console:

# Build TradeFed
m tradefed-all

# Launch the console
tradefed.sh

# Inside the console, run an XML configuration
tf > run /path/to/my/custom_test_config.xml

Mastering TradeFed is essential for Release Engineering and QA teams responsible for maintaining device stability in large-scale Android development environments.