Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion pytest_stf/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
39 changes: 35 additions & 4 deletions test/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand All @@ -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)

Expand Down