{"id":9125,"library":"mobly","title":"Mobly Automation Framework","description":"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.","status":"active","version":"1.13","language":"en","source_language":"en","source_url":"https://github.com/google/mobly","tags":["testing","automation","android","e2e","multi-device"],"install":[{"cmd":"pip install mobly","lang":"bash","label":"Install Mobly"}],"dependencies":[{"reason":"Requires Python 3.11 or newer.","package":"python","optional":false},{"reason":"Required for Android device control. Needs to be installed on the system PATH.","package":"adb","optional":false}],"imports":[{"symbol":"BaseTestClass","correct":"from mobly import base_test"},{"symbol":"test_runner","correct":"from mobly import test_runner"},{"symbol":"AndroidDevice","correct":"from mobly.controllers import android_device"},{"note":"The old snippet_client was deprecated in v1.12 and removed. Use snippet_client_v2 instead.","wrong":"from mobly.controllers.android_device_lib import snippet_client","symbol":"snippet_client_v2","correct":"from mobly.controllers.android_device_lib import snippet_client_v2"},{"note":"The direct import of snippet_event was deprecated in v1.12. Use mobly.snippet.callback_event instead.","wrong":"from mobly import snippet_event","symbol":"callback_event","correct":"from mobly.snippet import callback_event"},{"note":"The old callback_handler was deprecated in v1.12 and removed. Use callback_handler_v2 instead.","wrong":"from mobly import callback_handler","symbol":"callback_handler_v2","correct":"from mobly.controllers.android_device_lib import callback_handler_v2"}],"quickstart":{"code":"# sample_config.yml\nTestBeds:\n  - Name: SampleTestBed\n    Controllers:\n      AndroidDevice: '*'\n\n# hello_world_test.py\nfrom mobly import base_test\nfrom mobly import test_runner\nfrom mobly.controllers import android_device\n\nclass HelloWorldTest(base_test.BaseTestClass):\n    def setup_class(self):\n        # Registering android_device controller declares the test's dependency on Android devices.\n        # By default, we expect at least one device is found.\n        self.ads = self.register_controller(android_device)\n        self.dut = self.ads[0] # Get the first AndroidDevice object\n        # Start Mobly Bundled Snippets (MBS) if needed, using the updated MBS_PACKAGE constant\n        # You may need to install MBS on your device: \n        # https://github.com/google/mobly-bundled-snippets\n        try:\n            self.dut.load_snippet('mbs', android_device.MBS_PACKAGE)\n        except Exception as e:\n            self.log.warning(f\"Could not load MBS snippet. Make sure it's installed: {e}\")\n\n    def test_hello(self):\n        if hasattr(self.dut, 'mbs'):\n            self.dut.mbs.makeToast('Hello Mobly World!')\n        else:\n            self.log.info('MBS not loaded, skipping makeToast. Device detected.')\n\nif __name__ == '__main__':\n    # To run, save config as sample_config.yml and this file as hello_world_test.py\n    # Then execute: python hello_world_test.py -c sample_config.yml\n    test_runner.main()","lang":"python","description":"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."},"warnings":[{"fix":"Migrate all test code to Python 3. Mobly now requires Python 3.11 or newer.","message":"Python 2 support was completely removed in Mobly v1.11. All legacy Python 2 specific workarounds and APIs are no longer available.","severity":"breaking","affected_versions":">=1.11"},{"fix":"Upgrade your Python environment to 3.11 or later.","message":"The minimum required Python version for Mobly was bumped to 3.11 in v1.12.3.","severity":"breaking","affected_versions":">=1.12.3"},{"fix":"Update imports and code: `snippet_client` -> `snippet_client_v2`, `snippet_event` -> `mobly.snippet.callback_event`, `callback_handler` -> `callback_handler_v2`.","message":"The old Android snippet client (`snippet_client`), `snippet_event`, and `callback_handler` were deprecated in v1.12 and subsequently removed. The new generic Mobly snippet base client and Android snippet client (`snippet_client_v2`, `callback_handler_v2`) are now in use.","severity":"deprecated","affected_versions":">=1.12"},{"fix":"Rely on the operating system to allocate ports instead of using this function.","message":"The `get_available_host_port` function was deprecated in v1.12.3.","severity":"deprecated","affected_versions":">=1.12.3"},{"fix":"Refactor tests to use Mobly Snippet Lib or other modern Android interaction methods instead of SL4A.","message":"All SL4A (Scripting Layer for Android) related code was removed in Mobly v1.13.","severity":"breaking","affected_versions":">=1.13"},{"fix":"Handle exceptions within `teardown_class` explicitly if specific failure logic is required, or ensure `on_fail` logic is sufficient for other test stages.","message":"Errors raised from `teardown_class` methods do not trigger the `on_fail` callback.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Change the import to `from mobly.controllers.android_device_lib import snippet_client_v2` and update code to use `SnippetClientV2`.","cause":"Using the old, deprecated `snippet_client` import path which was removed after Mobly v1.12.","error":"ModuleNotFoundError: No module named 'mobly.snippet_client'"},{"fix":"Ensure you are importing and using `snippet_client_v2` correctly. The common pattern is `self.dut.load_snippet('your_snippet_name', 'com.your.package.name')` which internally uses `snippet_client_v2`.","cause":"Attempting to access `ad.snippet_client` on an `AndroidDevice` object after the `snippet_client` module was removed and replaced by `snippet_client_v2`.","error":"AttributeError: 'AndroidDevice' object has no attribute 'snippet_client'"},{"fix":"Verify `adb` is installed and working (`adb devices`), confirm USB debugging is enabled on the device, and ensure the device is properly connected and recognized. Check `adb logcat` for device-side issues.","cause":"The specified Android device (by serial or wildcard '*') is not detected by `adb devices` or is not in a ready state (e.g., USB debugging disabled, device offline).","error":"mobly.controllers.android_device.Error: Android device serial \"<SERIAL>\" is specified in config but is not reachable."},{"fix":"Upgrade your Python environment to version 3.11 or newer. You can use `pyenv` or `conda` for managing Python versions.","cause":"Running Mobly with an older Python version than required.","error":"RuntimeError: Python version must be >= 3.11"}]}