diff --git a/pytest_stf/plugin.py b/pytest_stf/plugin.py index b27f526..4c33340 100644 --- a/pytest_stf/plugin.py +++ b/pytest_stf/plugin.py @@ -49,6 +49,11 @@ def pytest_addoption(parser): "--appium_logs", help="Appium server log file path" ) + group.addoption( + "--stf_avoid_devices", + default=os.environ.get('STF_AVOID_DEVICES', None), + help="Comma-separated list of device serials to deprioritize during allocation", + ) def pytest_configure(config): @@ -113,9 +118,15 @@ def fixture_allocated_phone(pytestconfig, lockable): timeout = pytestconfig.getoption('stf_allocation_timeout') requirements = parse_requirements(requirements) + avoid_devices_raw = pytestconfig.getoption('stf_avoid_devices') + avoid_list = None + if avoid_devices_raw: + avoid_list = [s.strip() for s in avoid_devices_raw.split(',') if s.strip()] + with stf.allocation_context(requirements, wait_timeout=allocation_timeout, - timeout_seconds=timeout) as device: + timeout_seconds=timeout, + avoid_list=avoid_list) as device: yield device else: with lockable.auto_lock(requirements, allocation_timeout) as device: diff --git a/setup.py b/setup.py index 1f6828a..5c76bc0 100644 --- a/setup.py +++ b/setup.py @@ -22,7 +22,7 @@ install_requires=[ "pytest>=5.0", "pytest-metadata", - "stf-appium-client~=0.12.0", + "stf-appium-client @ git+https://github.com/kimikorento-o/stf-appium-python-client.git@44fec0f88626242c3538f482b7edbf9d5860f2f7", "pytest-lockable~=0.11.0"], # List additional groups of dependencies here (e.g. development # dependencies). Users will be able to install these using the "extras" diff --git a/test/test_plugin.py b/test/test_plugin.py index 903079f..fb2bc16 100644 --- a/test/test_plugin.py +++ b/test/test_plugin.py @@ -42,7 +42,11 @@ def test_plugin_stf(pytester: Pytester, example_stf): result = pytester.runpytest('--stf_host=.', '--stf_token=abc', '--phone_requirements=platform=Android') connect.assert_called_once_with('abc') - allocation_context.assert_called_once_with({'platform': 'Android'}, timeout_seconds=1000) + allocation_context.assert_called_once_with( + {'platform': 'Android'}, + wait_timeout=mock.ANY, + timeout_seconds=mock.ANY, + avoid_list=None) adb_start.assert_called_once_with() appium_server_start.assert_called_once_with() appium_server_stop.assert_called_once_with() @@ -53,6 +57,33 @@ def test_plugin_stf(pytester: Pytester, example_stf): result.assert_outcomes(passed=1) +def test_plugin_stf_with_avoid_devices(pytester: Pytester, example_stf): + """Make sure --stf_avoid_devices is parsed and passed to allocation_context""" + with mock.patch('stf_appium_client.StfClient.StfClient.connect'), \ + mock.patch('stf_appium_client.StfClient.StfClient.allocation_context') as allocation_context, \ + mock.patch('stf_appium_client.AdbServer.AdbServer.connect'), \ + mock.patch('stf_appium_client.AppiumServer.AppiumServer.start'), \ + mock.patch('stf_appium_client.AppiumServer.AppiumServer.stop'), \ + mock.patch('stf_appium_client.AppiumServer.AppiumServer.get_api_path'), \ + mock.patch('stf_appium_client.AppiumClient.AppiumClient.start'), \ + mock.patch('stf_appium_client.AppiumClient.AppiumClient.stop'): + + allocation_context.return_value.__enter__.return_value = { + "platform": "Android", + "remote_adb_url": "localhost"} + result = pytester.runpytest( + '--stf_host=.', '--stf_token=abc', + '--phone_requirements=platform=Android', + '--stf_avoid_devices=ABC123,DEF456') + + allocation_context.assert_called_once_with( + {'platform': 'Android'}, + wait_timeout=mock.ANY, + timeout_seconds=mock.ANY, + avoid_list=['ABC123', 'DEF456']) + result.assert_outcomes(passed=1) + + def test_plugin_local(pytester: Pytester, example_local): """Make sure the plugin works with a local device""" # run all tests with pytest @@ -61,9 +92,9 @@ def test_plugin_local(pytester: Pytester, example_local): mock.patch('stf_appium_client.AppiumClient.AppiumClient.start') as appium_client_start, \ mock.patch('stf_appium_client.AppiumClient.AppiumClient.stop') as appium_client_stop: - auto_lock.return_value.__enter__.return_value = { - "platform": "Android" - } + auto_lock.return_value.__enter__.return_value = mock.Mock( + resource_info={"platform": "Android"} + ) result = pytester.runpytest('--phone_requirements=platform=Android') print(result)