def test_connect_succes(self): mock_adb = Mock() mock_adb.get_devices.return_value = {'a': 12, 'b': 13, 'c': 123} Adb.adb = mock_adb Adb.connect(123) mock_adb.get_devices.assert_called_once()
def test_success_or_exception_succes(self, logger): input_string = 'Success' succes_msg = 'action succes' fail_msg = 'action fail' Adb.success_or_exception(input_string, succes_msg, fail_msg) logger.assert_called_once_with(succes_msg)
def test_connect_no_devices(self): mock_adb = Mock() mock_adb.get_devices.return_value = {} Adb.adb = mock_adb with pytest.raises(Adb.ConnectionError): Adb.connect('123') mock_adb.get_devices.assert_called_once()
def test_setup_succes_default_path(self, adb): adb_instance = MagicMock() adb_instance._ADB__error = None adb.return_value = adb_instance Adb.setup() assert isinstance(Adb.adb, MagicMock) adb.assert_called_once_with(adb_path='adb')
def test_connect_device_missing(self): mock_adb = Mock() mock_adb.get_devices.return_value = {'a': 12, 'b': 13} Adb.adb = mock_adb with pytest.raises(Adb.ConnectionError): Adb.connect(123) mock_adb.get_devices.assert_called_once()
def test_success_or_exception_exception(self, logger): input_string = 'fail' succes_msg = 'action succes' fail_msg = 'action fail' with pytest.raises(Adb.AdbError): Adb.success_or_exception(input_string, succes_msg, fail_msg) logger.assert_called_once_with( fail_msg + '\nMessage returned:\n{}'.format(input_string))
def test_install_multiple_no_apks_in_xapk_file(self, zipfile, tmpdir): xapk_file = tmpdir.mkdir("xapk").join("test_apk.xapk") xapk_file.write("This is an xapk file") device_id = 123 mock_adb = Mock() mock_adb._ADB__output = 'succes' Adb.adb = mock_adb with pytest.raises(ConfigError): Adb.install(device_id, xapk_file)
def test_configure_settings(self, shell): Adb.adb = Mock() device_id = 123 setting1 = "location_high_accuracy" setting2 = "location_gps_only" Adb.configure_settings(device_id, setting1, enable=True) shell.assert_called_with( 123, "settings put secure location_providers_allowed +gps,network") Adb.configure_settings(device_id, setting2, enable=False) shell.assert_called_with( 123, "settings put secure location_providers_allowed -gps")
def test_shell_su_error(self): mock_adb = Mock() mock_adb.shell_command.return_value = "su_error" Adb.adb = mock_adb with pytest.raises(Adb.AdbError): Adb.shell_su(123, "test_command_su") expected_calls = [ call.set_target_by_name(123), call.shell_command('su -c \'test_command_su\'') ] assert mock_adb.mock_calls == expected_calls
def test_list_apps(self, adb_shell): adb_shell.return_value = 'package:com.app.1\npackage:com.app.2\npackage:com.app.3' result = Adb.list_apps(123) adb_shell.assert_called_once_with(123, 'pm list packages') assert len(result) == 3 assert 'com.app.1' in result assert 'com.app.2' in result assert 'com.app.3' in result
def test_shell_su_succes(self): mock_adb = Mock() mock_adb.shell_command.return_value = "su_succes " Adb.adb = mock_adb result = Adb.shell_su(123, "test_command_su") expected_calls = [ call.set_target_by_name(123), call.shell_command('su -c \'test_command_su\'') ] assert mock_adb.mock_calls == expected_calls assert result == 'su_succes'
def test_uninstall_keep_data(self, s_or_e): mock_adb = Mock() mock_adb.uninstall.return_value = 'succes' Adb.adb = mock_adb device_id = 123 name = 'app_name' manager = Mock() manager.attach_mock(s_or_e, "s_or_e_mock") manager.mock_adb = mock_adb Adb.uninstall(device_id, name, True) expected_calls = [ call.mock_adb.set_target_by_name(123), call.mock_adb.uninstall(package=name, keepdata=False), call.s_or_e_mock( 'succes', '{}: "{}" uninstalled'.format(device_id, name), '{}: Failed to uninstall "{}"'.format(device_id, name)) ] assert manager.mock_calls == expected_calls
def test_clear_app_data(self, s_or_e): mock_adb = Mock() mock_adb.shell_command.return_value = 'succes' Adb.adb = mock_adb device_id = 123 name = 'app_name' manager = Mock() manager.attach_mock(s_or_e, "s_or_e_mock") manager.mock_adb = mock_adb Adb.clear_app_data(device_id, name) expected_calls = [ call.mock_adb.set_target_by_name(123), call.mock_adb.shell_command('pm clear app_name'), call.s_or_e_mock( 'succes', '{}: Data of "{}" cleared'.format(device_id, name), '{}: Failed to clear data for "{}"'.format(device_id, name)) ] assert manager.mock_calls == expected_calls
def test_logcat_no_regex(self): mock_adb = Mock() mock_adb.return_value = 'get_logcat output' Adb.shell = mock_adb device_id = 123 result = Adb.logcat(device_id) assert result == 'get_logcat output' expected_calls = [call.shell(device_id, f"logcat -d")] assert mock_adb.mock_calls == expected_calls
def test_logcat_with_regex(self): mock_adb = Mock() mock_adb.return_value = 'get_logcat output' Adb.shell = mock_adb test_regex = '[a-zA-Z]+' device_id = 123 result = Adb.logcat(device_id, test_regex) assert result == 'get_logcat output' expected_calls = [ call.shell(device_id, f'logcat -d | grep "{test_regex}"') ] assert mock_adb.mock_calls == expected_calls
def test_logcat_no_regex(self): mock_adb = Mock() mock_adb.get_logcat.return_value = 'get_logcat output' Adb.adb = mock_adb device_id = 123 result = Adb.logcat(device_id) assert result == 'get_logcat output' expected_calls = [ call.set_target_by_name(device_id), call.get_logcat(lcfilter='-d') ] assert mock_adb.mock_calls == expected_calls
def test_install_not_all_permissions(self): mock_adb = Mock() mock_adb._ADB__output = 'succes' Adb.adb = mock_adb device_id = 123 apk = 'test_apk.apk' result = Adb.install(device_id, apk, all_permissions=False) assert result == 'succes' expected_calls = [ call.set_target_by_name(device_id), call.run_cmd('install -r {}'.format(apk)) ] assert mock_adb.mock_calls == expected_calls
def test_logcat_with_regex(self): mock_adb = Mock() mock_adb.get_logcat.return_value = 'get_logcat output' Adb.adb = mock_adb test_regex = '[a-zA-Z]+' device_id = 123 result = Adb.logcat(device_id, test_regex) assert result == 'get_logcat output' expected_calls = [ call.set_target_by_name(device_id), call.get_logcat(lcfilter='-d -e {}'.format(test_regex)) ] assert mock_adb.mock_calls == expected_calls
def test_push(self): mock_adb = Mock() mock_adb._ADB__output = 'push output' Adb.adb = mock_adb device_id = 123 local_path = 'local/path' remote_path = 'remote/path' result = Adb.push(device_id, local_path, remote_path) assert result == 'push output' expected_calls = [ call.set_target_by_name(device_id), call.run_cmd('push {} {}'.format(local_path, remote_path)) ] assert mock_adb.mock_calls == expected_calls
def test_install_multiple_default(self, listdir, getcwd): mock_adb = Mock() mock_adb._ADB__output = 'succes' Adb.adb = mock_adb device_id = 123 apk = 'test_apk.xapk' getcwd.return_value = '.' listdir.return_value = [apk] result = Adb.install(device_id, apk) assert result == 'succes' expected_calls = [ call.set_target_by_name(device_id), call.run_cmd('install-multiple -r -g {}'.format(apk)) ] assert mock_adb.mock_calls == expected_calls
def test_pull_error_with_bytes_in(self): mock_adb = Mock() mock_adb._ADB__output = 'pull output' mock_adb._ADB__error = 'bytes in error' Adb.adb = mock_adb device_id = 123 local_path = 'local/path' remote_path = 'remote/path' result = Adb.pull(device_id, remote_path, local_path) assert result == 'bytes in error' expected_calls = [ call.set_target_by_name(device_id), call.run_cmd('pull {} {}'.format(remote_path, local_path)) ] assert mock_adb.mock_calls == expected_calls
def test_install_multiple_default(self, zipfile, tmpdir): xapk_file = tmpdir.mkdir("xapk").join("test_apk.xapk") apk_file = tmpdir.mkdir("xapk/test_apk/").join("test_apk.apk") xapk_file.write("This is an xapk file") apk_file.write("This is an apk file") device_id = 123 mock_adb = Mock() mock_adb._ADB__output = 'succes' Adb.adb = mock_adb result = Adb.install(device_id, xapk_file) assert result == 'succes' expected_calls = [ call.set_target_by_name(device_id), call.run_cmd('install-multiple -r -g {}'.format(apk_file)) ] assert mock_adb.mock_calls == expected_calls
def test_reset_true(self): Adb.adb = Mock() cmd = True Adb.reset(cmd) expected_calls = [call.kill_server(), call.get_devices()] assert Adb.adb.mock_calls == expected_calls
def test_reset_false(self): Adb.adb = Mock() cmd = False Adb.reset(cmd) expected_calls = [] assert Adb.adb.mock_calls == expected_calls
def test_setup_error(self, adb): adb_instance = MagicMock() adb_instance._ADB__error = True adb.return_value = adb_instance with pytest.raises(Adb.AdbError): Adb.setup()