This tutorial shows how to write and execute Mobly tests for running Android instrumentation tests. For more details about instrumentation tests, please refer to https://developer.android.com/studio/test/index.html.
adb devices
.Here are the names that we use in this tutorial, substitute these names with your actual apk package and file names when using your real files:
application.apk
instrumentation_test.apk
com.example.package.test
Assuming your apks are already installed on devices. You can just subclass the instrumentation test class and run against your package.
You will need a configuration file for Mobly to find your devices.
sample_config.yml
TestBeds: - Name: BasicTestBed Controllers: AndroidDevice: '*'
instrumentation_test.py
from mobly import base_instrumentation_test from mobly import test_runner from mobly.controllers import android_device class InstrumentationTest(base_instrumentation_test.BaseInstrumentationTestClass): def setup_class(self): self.dut = self.register_controller(android_device)[0] def test_instrumentation(self): self.run_instrumentation_test(self.dut, 'com.example.package.test') if __name__ == '__main__': test_runner.main()
To execute:
$ python instrumentation_test.py -c sample_config.yml
Expect:
The output from normally running your instrumentation tests along with a summary of the test results.
If your instrumentation tests use instrumentation options for controlling behaviour, then you can put these options into your configuration file and then fetch them when you run your instrumentatation tests.
sample_config.yml
TestBeds: - Name: BasicTestBed Controllers: AndroidDevice: '*' TestParams: instrumentation_option_annotation: android.support.test.filters.LargeTest instrumentation_option_nonAnnotation: android.support.test.filters.SmallTest
instrumentation_test.py
from mobly import base_instrumentation_test from mobly import test_runner from mobly.controllers import android_device class InstrumentationTest(base_instrumentation_test.BaseInstrumentationTestClass): def setup_class(self): self.dut = self.register_controller(android_device)[0] self.options = self.parse_instrumentation_options(self.user_params) def test_instrumentation(self): self.run_instrumentation_test(self.dut, 'com.example.package.test', options=self.options) if __name__ == '__main__': test_runner.main()
To execute:
$ python instrumentation_test.py -c sample_config.yml
Expect:
The output of your LargeTest instrumentation tests with no SmallTest instrumentation test being run.
If you have a custom runner that you use for instrumentation tests, then you can specify it in the run_instrumentation_test method call. Replace com.example.package.test.CustomRunner
with the fully quailied package name of your real instrumentation runner.
def test_instrumentation(self): self.run_instrumentation_test(self.dut, 'com.example.package.test', runner='com.example.package.test.CustomRunner')
If you have multiple devices that you want to run instrumentation tests against, then you can simply call the run_instrumentation_test method multiple times. If you need to distinguish between runs, then you can specify a prefix.
sample_config.yml
TestBeds: - Name: TwoDeviceTestBed Controllers: AndroidDevice: - serial: xyz label: dut - serial: abc label: dut
instrumentation_test.py
from mobly import base_instrumentation_test from mobly import test_runner from mobly.controllers import android_device class InstrumentationTest(base_instrumentation_test.BaseInstrumentationTestClass): def setup_class(self): self.ads = self.register_controller(android_device) # Get all of the dut devices to run instrumentation tests against. self.duts = android_device.get_devices(self.ads, label='dut') def test_instrumentation(self): # Iterate over the dut devices with a corresponding index. for index, dut in enumerate(self.duts): # Specify a prefix to help disambiguate the runs. self.run_instrumentation_test(dut, 'com.example.package.tests', prefix='test_run_%s' % index) if __name__ == '__main__': test_runner.main()
To execute:
$ python instrumentation_test.py -c sample_config.yml
Expect:
The output from both instrumentation runs along with an aggregated summary of the results from both runs.