Include unit and functional tests

This section explains how to run and contribute to the Trade Federation tests after making a change to the project. This includes:

  • Where to add the new unit test classes
  • Running unit tests in Eclipse and outside the Eclipse IDE
  • Where to add functional tests
  • Running the functional tests
  • Running some of TF presubmit validation locally

Add unit tests

In the Android Open Source Project (AOSP), add the unit tests class in: tools/tradefederation/core/tests/src/com/android/tradefed/UnitTests.java

IMPORTANT: Adding your new unit test classes to these locations will result in them automatically running in presubmit without additional setup.

Run unit tests

All the AOSP unit tests and functional tests for Trade Federation are located in the tools/tradefederation/core/tests project.

Inside Eclipse, to run an individual unit test, simply right-click the test and select Run As > JUnit. To run all unit tests, run the com.android.tradefed.UnitTests suite.

You can also start the unit test from the command line in the Tradefed source tree after building, like so: tools/tradefederation/core/javatests/run_tradefed_tests.sh

Unit tests can be executed standalone, but functional tests should be executed by using Trade Federation itself; they require an Android device. All functional tests should follow the naming convention *FuncTest.

Check unit tests results

When running run_tradefed_tests.sh, the unit tests are exercising all parts of the test harness including some error conditions. It will print verbose output to the console, including stack traces.

The final summary of the results will indicate if a failure occurred or not.

Example final summary in the console:

12:55:02 ========== Result Summary ==========
Results summary for test-tag 'TFUnitTests': 3355 Tests [3355 Passed]

All tests are maintained to pass, so if any failure occurs on your local change please ensure the tests are fixed.

Add functional tests

  • If your functional test involves a device (uses any ITestDevice API) the suite definition is located in com.android.tradefed.DeviceFuncTests. Otherwise, the suite definition is located in com.android.tradefed.FuncTests.

  • If your test methods make sense to put in one of the existing suite sub-classes, prefer to add it there. Otherwise, feel free add a new class to the applicable suite.

  • In either case, by adding it to the suite, your test is automatically run in the CI pipeline with the other functional tests.

Run functional tests

To run a functional test from Eclipse:

  1. Ensure a device is connected to the host and that adb and if necessary fastboot are in Eclipse's PATH. The easiest way to do this is to launch Eclipse from a shell setup with the proper PATH.
  2. Create a Java application. Run configuration via Run > Run configurations.
  3. Set project to tradefed-tests and the main class to com.android.tradefed.command.CommandRunner.
  4. Run m tradefed-all.
  5. Provide the following command line arguments in the Arguments tab: host --class <full path of test class to run>
  6. Click Run.

To run functional tests outside Eclipse.

  1. Build Trade Federation.
  2. Connect an Android device to the host.
  3. Run tools/tradefederation/core/javatests/run_tradefed_func_tests.sh
  4. Optionally, choose the device by appending --serial <serial no> as it appears in the output of adb devices.

Run TF presubmit tests against local changes

If you want to run in a similar way as the TF presubmit, use this:

tools/tradefederation/core/javatests/run_tradefed_aosp_presubmit.sh

This will trigger all the TF presubmit tests against your locally built TF to help you validate that your change is not breaking any tests.

TF presubmit tests are a superset of the unit tests above, but it is slower to run them. So it is recommended to run the unit tests during development for quicker validation and to run the presubmits before uploading the CL.