View a markdown version of this page

Integrating Android Instrumentation with Device Farm - AWS Device Farm

Integrating Android Instrumentation with Device Farm

Note

Use the following instructions to integrate Android instrumentation tests with AWS Device Farm. For more information about using instrumentation tests in Device Farm, see Instrumentation for Android and AWS Device Farm.

Configure instrumentation test reports using a custom test environment

Any file that your tests write to $DEVICEFARM_LOG_DIR in your test spec YAML file is available in your test output artifacts after the run completes. By default, Device Farm also generates its own test report, called test insights. Test insights give you a per-test breakdown of results, durations, and stack traces for failures. You can view test insights in the console, and through the AWS CLI and APIs. Test insights are in addition to the artifacts that your tests produce, not a replacement for them.

Before you begin, make sure that your run uses a custom test environment. Device Farm does not generate test insights in the standard test environment. For more information, see Test environments in AWS Device Farm.

To configure test reports for your instrumentation tests:

  1. Run am instrument with the -r option, and pipe its output through tee so that the output appears in your test spec output. Device Farm reads your test spec output to build the report. The default instrumentation test spec already does this:

    adb -s $DEVICEFARM_DEVICE_UDID shell am instrument -r -w --no-window-animation \ $DEVICEFARM_TEST_PACKAGE_NAME/$DEVICEFARM_TEST_PACKAGE_RUNNER 2>&1 | tee $DEVICEFARM_LOG_DIR/instrument.log

    The -r option tells am instrument to print raw results. tee writes those results to a file and also passes them through to your test spec output, which is what Device Farm reads. The results look like the following:

    INSTRUMENTATION_STATUS: class=com.example.GpsConnectInstrumentedTest INSTRUMENTATION_STATUS: current=1 INSTRUMENTATION_STATUS: id=AndroidJUnitRunner INSTRUMENTATION_STATUS: numtests=2 INSTRUMENTATION_STATUS: test=useAppContext

    If you redirect this output to a file only, with > instead of tee, the results never reach your test spec output and Device Farm cannot build a report.

  2. Schedule the run in a custom test environment. Test insights are on by default. If you don't set the insightsTypes field of the configuration parameter of your ScheduleRun request, Device Farm sets it to TEST_REPORT. To turn off test insights for a run, see Creating a test run in Device Farm.

Device Farm timestamps each line of your test spec output, so the report has a start time for each test. When the run records video, you also get per-test video. For more information, see Per-test video.

To view the report after the run completes, see View a test report (console). If Device Farm does not generate a report, see Troubleshooting test reports and insights in AWS Device Farm.

Run Android Instrumentation tests (console)

Use the Device Farm console to upload your tests.

  1. Sign in to the Device Farm console at https://console.aws.amazon.com/devicefarm.

  2. In the navigation pane, choose Mobile Device Testing, and then choose Projects.

  3. In the list of projects, choose the project that you want to upload your tests to.

    Tip

    You can use the search bar to filter the project list by name.

    To create a project, follow the instructions in Creating a project in AWS Device Farm.

  4. Select Create run.

  5. Under Select app and run type, in the Run type section, select Android app.

  6. Under Select app, in the App selection options section, choose Select sample app provided by Device Farm if you do not have an app. If you are bringing your own app, select Upload own app, and then choose your APK (.apk file format).

  7. Under Configure test, in the Select test framework section, choose Instrumentation, and then choose Choose File. Browse to and choose the APK file (.apk file format) that contains your tests.

  8. Under Choose your execution environment, select either Run your test in a custom environment or Run your test in our standard environment. For more information, see Test environments in AWS Device Farm.

  9. If you chose the custom environment, you can use the default test spec that is populated for instrumentation tests, or choose Upload own test spec to provide your own.

  10. Under Select devices, choose a device selection method. Select Use Device Pool to choose from a curated collection of devices or a custom device pool you created. Select Manually select devices to pick individual devices to run your tests against. The Device compatibility section shows how many devices in the selected pool are compatible with your app. For more information, see Device support in AWS Device Farm.

  11. To configure run-level properties, update the Run settings section.

  12. Complete the remaining steps, and then start the run.

View a test report (console)

  1. Sign in to the Device Farm console at https://console.aws.amazon.com/devicefarm.

  2. In the navigation pane, choose Mobile Device Testing, and then choose Projects.

  3. Choose the project that contains the run you want to inspect.

  4. Choose the completed run to open its details.

  5. Choose one of the completed jobs to open the results for that device.

With test insights enabled

The job results include a Test report tab. Choose it to see a per-test breakdown. The following screenshots show the Test report tab with all columns visible.

The first set of columns on the Test report tab for a completed instrumentation job.
The remaining columns on the Test report tab for a completed instrumentation job.

When the run records a video, the Video column has a play button for each test that plays the test in the job video. For more information, see Watch a test in the job video.

The following screenshot shows the Test report tab with the Test video panel open.

The Test report tab for a completed Android instrumentation job, with the Test video panel open on the selected test.

The tab shows the following fields for each test:

testName

The name of the test method.

testClass

The name of the test class.

result

The Device Farm result for the test.

frameworkResult

The result that the instrumentation framework reported. Device Farm maps this value to the normalized result field.

durationSeconds

The duration of the test, in seconds.

startTimestamp

The time when the test started.

endTimestamp

The time when the test ended.

current

The position of the test in the run.

numTests

The total number of tests in the run.

statusCode

The instrumentation status code that the test reported.

stream

A human-readable version of the instrumentation output for the test.

stackTrace

For a failed test, the stack trace of the failure.

videoStartSeconds

Where the test started in the job video, in seconds. See Per-test video.

videoEndSeconds

Where the test ended in the job video, in seconds.

To download the full test report as a JSON file, choose Download full summary at the top of the job details.

To choose which columns appear, choose the gear icon. In the settings, you can select the columns to display and turn Group by class on or off. Group by class is on by default, which groups the tests by their test class. Turn it off to see a flat list of all tests, as shown in the following screenshot.

The Test report tab with Group by class turned off, showing a flat list of tests.

Without test insights enabled

If the run used the standard test environment, or if test insights were turned off for the run, the job results show the standard test output and artifacts, but no Test report tab. To get a test report, schedule a new run in a custom test environment with Generate test report selected.

The job results for a completed instrumentation job without test insights enabled.

View a test report (AWS CLI)

Run get-job and specify the job ARN:

aws devicefarm get-job --arn arn:aws:devicefarm:us-west-2:123456789012:job:PROJECT_ID/RUN_ID/00000

Without test insights enabled

If the run did not generate test insights, for example because it used the standard test environment or you turned off test insights, the response contains the standard job fields, such as the job status, result, counters, and device:

{ "job": { "arn": "arn:aws:devicefarm:us-west-2:123456789012:job:EXAMPLE-PROJECT/EXAMPLE-RUN/00000", "name": "Example Android Phone", "created": "2026-08-05T14:26:56.959000-07:00", "status": "COMPLETED", "result": "PASSED", "counters": { "total": 3, "passed": 3, "failed": 0, "warned": 0, "errored": 0, "stopped": 0, "skipped": 0 }, "message": "Successful test lifecycle of Setup Test", "device": { "arn": "arn:aws:devicefarm:us-west-2::device:EXAMPLEDEVICEID", "name": "Example Android Phone", "platform": "ANDROID", "os": "16", "formFactor": "PHONE", "fleetType": "PUBLIC" }, "deviceMinutes": { "total": 1.38, "metered": 0.0, "unmetered": 1.13 }, "videoCapture": true } }

With test insights enabled

If the run generated test insights, the response also includes an insights object. This object contains the test report status, high-level metrics, and a presigned URL to the detailed report:

{ "job": { "arn": "arn:aws:devicefarm:us-west-2:123456789012:job:EXAMPLE-PROJECT/EXAMPLE-RUN/00000", "status": "COMPLETED", "result": "PASSED", "counters": { ... }, "device": { ... }, "deviceMinutes": { ... }, "videoCapture": true, "insights": { "status": "COMPLETED", "testReport": { "message": "Results: 2 Executed | 2 passed, Median test duration: 28.926 seconds.", "metrics": { "testsTotal": 2, "testsPassed": 2, "testsFailed": 0, "testsSkipped": 0, "testsErrored": 0, "testsOther": 0, "testsPassedPercentage": 100.0 }, "testDetailsUrl": "https://EXAMPLE-PRESIGNED-URL" } } } }

The testDetailsUrl field is a presigned URL to the full test report JSON. Download it to get the per-test breakdown:

curl -o test-report.json "PRESIGNED_URL"

The following is an example test report for an instrumentation job:

{ "version": "1.0", "jobArn": "arn:aws:devicefarm:us-west-2:123456789012:job:EXAMPLE-PROJECT/EXAMPLE-RUN/00000", "metrics": { "testsTotal": 3, "testsPassed": 2, "testsFailed": 1, "testsSkipped": 0, "testsErrored": 0, "testsOther": 0, "testsPassedPercentage": 66.67, "totalTestExecutionDurationSeconds": 26.004, "medianTestExecutionDurationSeconds": 1.598 }, "testDetails": [ { "testName": "testConnect", "testClass": "com.example.myapp.GpsConnectInstrumentedTest", "frameworkResult": "PASSED", "result": "PASSED", "durationSeconds": 24.399, "startTimestamp": "2026-08-05T20:38:30.131514Z", "endTimestamp": "2026-08-05T20:38:54.530369Z", "statusCode": 0, "current": 1, "numTests": 3 }, { "testName": "testHistoryOff", "testClass": "com.example.myapp.NotificationHistoryInstrumentedTest", "frameworkResult": "PASSED", "result": "PASSED", "durationSeconds": 1.598, "startTimestamp": "2026-08-05T20:38:54.531765Z", "endTimestamp": "2026-08-05T20:38:56.130032Z", "statusCode": 0, "current": 2, "numTests": 3 }, { "testName": "testConnect", "testClass": "com.example.myapp.WifiConnectInstrumentedTest", "frameworkResult": "FAILED", "result": "FAILED", "durationSeconds": 0.002, "startTimestamp": "2026-08-05T20:38:56.131857Z", "endTimestamp": "2026-08-05T20:38:56.133770Z", "stackTrace": "java.lang.NullPointerException: Attempt to invoke virtual method 'boolean androidx.test.uiautomator.UiDevice.pressHome()' on a null object reference\n\tat com.example.myapp.WifiConnectInstrumentedTest.testConnect(WifiConnectInstrumentedTest.java:93)\n\t...", "stream": "\nError in testConnect(com.example.myapp.WifiConnectInstrumentedTest):\njava.lang.NullPointerException: Attempt to invoke virtual method 'boolean androidx.test.uiautomator.UiDevice.pressHome()' on a null object reference\n\t...", "statusCode": -2, "current": 3, "numTests": 3 } ], "errorMessage": "There was 1 failure:\n1) testConnect(com.example.myapp.WifiConnectInstrumentedTest)\njava.lang.NullPointerException: Attempt to invoke virtual method 'boolean androidx.test.uiautomator.UiDevice.pressHome()' on a null object reference\n\t...\nFAILURES!!!\nTests run: 3, Failures: 1", "instrumentationCode": -1 }

The report contains the following top-level fields:

version

The report schema version.

jobArn

The ARN of the job.

metrics

Aggregate results for the job. The metrics object contains the following fields:

testsTotal

The total number of tests in the job.

testsPassed

The number of tests that passed.

testsFailed

The number of tests that failed.

testsSkipped

The number of tests that were skipped.

testsErrored

The number of tests that errored.

testsOther

The number of tests with another result.

testsPassedPercentage

The percentage of tests that passed.

totalTestExecutionDurationSeconds

The total duration of all tests, in seconds.

medianTestExecutionDurationSeconds

The median duration of a test, in seconds.

errorMessage

If the run had failures, an error message that summarizes them.

instrumentationCode

The overall instrumentation exit code for the job.

testDetails

A list of per-test results. Each entry in testDetails contains the following fields:

testName

The name of the test method.

testClass

The name of the test class.

result

The Device Farm result for the test.

frameworkResult

The result that the instrumentation framework reported. Device Farm maps this value to the normalized result field.

durationSeconds

The duration of the test, in seconds.

startTimestamp

The time when the test started.

endTimestamp

The time when the test ended.

current

The position of the test in the run.

numTests

The total number of tests in the run.

statusCode

The instrumentation status code that the test reported.

stream

A human-readable version of the instrumentation output for the test.

stackTrace

For a failed test, the stack trace of the failure.

videoStartSeconds

Where the test started in the job video, in seconds. See Per-test video.

videoEndSeconds

Where the test ended in the job video, in seconds.

(Optional) Take screenshots in Android instrumentation tests

You can take screenshots as part of your Android Instrumentation tests.

To take screenshots, call one of the following methods:

  • For Robotium, call the takeScreenShot method (for example, solo.takeScreenShot();).

  • For Spoon, call the screenshot method, for example:

    Spoon.screenshot(activity, "initial_state"); /* Normal test code... */ Spoon.screenshot(activity, "after_login");

During a test run, Device Farm gets screenshots from the following locations on the devices, if they exist, and then adds them to the test reports:

  • /sdcard/robotium-screenshots

  • /sdcard/test-screenshots

  • /sdcard/Download/spoon-screenshots/test-class-name/test-method-name

  • /data/data/application-package-name/app_spoon-screenshots/test-class-name/test-method-name