Mobly Automation Framework

1.13 · active · verified Thu Apr 16

Mobly is an open-source, Python-based test framework for host-driven, end-to-end automated testing, specializing in scenarios requiring multiple devices, complex environments, or custom hardware setups. It is currently at version 1.13 and has a regular release cadence, with recent releases focusing on improvements, bug fixes, and Python 3.11+ compatibility.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates a basic Mobly test that registers an Android device controller and attempts to display a 'Hello World!' toast message on the device using Mobly Bundled Snippets (MBS). It requires a `sample_config.yml` file to specify the testbed configuration. Ensure 'adb' is configured and 'USB debugging' is enabled on the Android device.

# sample_config.yml
TestBeds:
  - Name: SampleTestBed
    Controllers:
      AndroidDevice: '*'

# hello_world_test.py
from mobly import base_test
from mobly import test_runner
from mobly.controllers import android_device

class HelloWorldTest(base_test.BaseTestClass):
    def setup_class(self):
        # Registering android_device controller declares the test's dependency on Android devices.
        # By default, we expect at least one device is found.
        self.ads = self.register_controller(android_device)
        self.dut = self.ads[0] # Get the first AndroidDevice object
        # Start Mobly Bundled Snippets (MBS) if needed, using the updated MBS_PACKAGE constant
        # You may need to install MBS on your device: 
        # https://github.com/google/mobly-bundled-snippets
        try:
            self.dut.load_snippet('mbs', android_device.MBS_PACKAGE)
        except Exception as e:
            self.log.warning(f"Could not load MBS snippet. Make sure it's installed: {e}")

    def test_hello(self):
        if hasattr(self.dut, 'mbs'):
            self.dut.mbs.makeToast('Hello Mobly World!')
        else:
            self.log.info('MBS not loaded, skipping makeToast. Device detected.')

if __name__ == '__main__':
    # To run, save config as sample_config.yml and this file as hello_world_test.py
    # Then execute: python hello_world_test.py -c sample_config.yml
    test_runner.main()

view raw JSON →