Target preparers are invoked before the tests in the test level in which they are defined. This allows the setup of any device for tests to run smoothly.
Base interface
The base interface is
ITargetPreparer,
which allows implementation of a setUp method to be executed. We
recommend implementing our basic abstract class
BaseTargetPreparer,
which provides a built-in disablement feature to easily disable a preparer.
Cleaner interface
The natural extension of setUp is tearDown and is provided by a different
interface,
ITargetCleaner. That provides the tearDown interface
that allows cleaning up anything that was done in setUp after the test
execution.
The BaseTargetPreparer class also extends ITargetCleaner.
Recommendations
We recommend each preparer be limited to a single main function, for example installing an APK or running a command. This allows for easier reuse of preparers.
Also check the list of available preparers before adding a new one to
avoid duplicating work. Preparers are available in tools/tradefederation/core/src/com/android/tradefed/targetprep/.
XML configuration
The object tag is target_preparer, for example:
<target_preparer class="com.android.tradefed.targetprep.InstallApkSetup">
<option name="install-arg" value="-d"/>
</target_preparer>
Also refer to Set up suites for context.
Top-level setup
If specified in a top-level setup, the preparer runs only once for each
device. An example is
cts-common.xml,
which is a top-level setup for Android Compatibility Test Suite (CTS) tests.
Module-level setup
If specified at the module level, the preparer always runs before that
module. An example is
backup/AndroidTest.xml,
which defines how Tradefed runs the backup CTS module.
Note that while the preparer runs before the module, it runs after any system status checkers.