tests: Translate platform markers to skipif marks.
This means pytest shows the correct location and the marks work correctly when marking individual items in a parametrize marker.
This commit is contained in:
parent
18bd20e109
commit
240e271b0d
@ -41,16 +41,52 @@ from qutebrowser.utils import objreg
|
|||||||
from PyQt5.QtNetwork import QNetworkCookieJar
|
from PyQt5.QtNetwork import QNetworkCookieJar
|
||||||
|
|
||||||
|
|
||||||
def pytest_collection_modifyitems(items):
|
def _apply_platform_markers(item):
|
||||||
"""Automatically add a 'gui' marker to all gui-related tests.
|
"""Apply a skip marker to a given item."""
|
||||||
|
markers = [
|
||||||
|
('posix', os.name != 'posix', "Requires a POSIX os"),
|
||||||
|
('windows', os.name != 'nt', "Requires Windows"),
|
||||||
|
('linux', not sys.platform.startswith('linux'), "Requires Linux"),
|
||||||
|
('osx', sys.platform != 'darwin', "Requires OS X"),
|
||||||
|
('not_osx', sys.platform == 'darwin', "Skipped on OS X"),
|
||||||
|
('not_frozen', getattr(sys, 'frozen', False),
|
||||||
|
"Can't be run when frozen"),
|
||||||
|
('frozen', not getattr(sys, 'frozen', False),
|
||||||
|
"Can only run when frozen"),
|
||||||
|
]
|
||||||
|
|
||||||
|
for searched_marker, condition, default_reason in markers:
|
||||||
|
marker = item.get_marker(searched_marker)
|
||||||
|
if not marker:
|
||||||
|
continue
|
||||||
|
|
||||||
|
if 'reason' in marker.kwargs:
|
||||||
|
reason = '{}: {}'.format(default_reason,
|
||||||
|
marker.kwargs['reason'])
|
||||||
|
del marker.kwargs['reason']
|
||||||
|
else:
|
||||||
|
reason = default_reason + '.'
|
||||||
|
skipif_marker = pytest.mark.skipif(condition, *marker.args,
|
||||||
|
reason=reason, **marker.kwargs)
|
||||||
|
item.add_marker(skipif_marker)
|
||||||
|
|
||||||
|
|
||||||
|
def pytest_collection_modifyitems(items):
|
||||||
|
"""Handle custom markers.
|
||||||
|
|
||||||
|
pytest hook called after collection has been performed.
|
||||||
|
|
||||||
|
Adds a marker named "gui" which can be used to filter gui tests from the
|
||||||
|
command line.
|
||||||
|
|
||||||
pytest hook called after collection has been performed, adds a marker
|
|
||||||
named "gui" which can be used to filter gui tests from the command line.
|
|
||||||
For example:
|
For example:
|
||||||
|
|
||||||
py.test -m "not gui" # run all tests except gui tests
|
py.test -m "not gui" # run all tests except gui tests
|
||||||
py.test -m "gui" # run only gui tests
|
py.test -m "gui" # run only gui tests
|
||||||
|
|
||||||
|
It also handles the platform specific markers by translating them to skipif
|
||||||
|
markers.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
items: list of _pytest.main.Node items, where each item represents
|
items: list of _pytest.main.Node items, where each item represents
|
||||||
a python test that will be executed.
|
a python test that will be executed.
|
||||||
@ -78,35 +114,7 @@ def pytest_collection_modifyitems(items):
|
|||||||
if module_root_dir == 'integration':
|
if module_root_dir == 'integration':
|
||||||
item.add_marker(pytest.mark.integration)
|
item.add_marker(pytest.mark.integration)
|
||||||
|
|
||||||
|
_apply_platform_markers(item)
|
||||||
def pytest_runtest_setup(item):
|
|
||||||
"""Add some custom markers."""
|
|
||||||
if not isinstance(item, item.Function):
|
|
||||||
return
|
|
||||||
|
|
||||||
markers = [
|
|
||||||
('posix', os.name != 'posix', "Requires a POSIX os"),
|
|
||||||
('windows', os.name != 'nt', "Requires Windows"),
|
|
||||||
('linux', not sys.platform.startswith('linux'), "Requires Linux"),
|
|
||||||
('osx', sys.platform != 'darwin', "Requires OS X"),
|
|
||||||
('not_osx', sys.platform == 'darwin', "Skipped on OS X"),
|
|
||||||
('not_frozen', getattr(sys, 'frozen', False),
|
|
||||||
"Can't be run when frozen"),
|
|
||||||
('frozen', not getattr(sys, 'frozen', False),
|
|
||||||
"Can only run when frozen"),
|
|
||||||
]
|
|
||||||
|
|
||||||
for searched_marker, condition, default_reason in markers:
|
|
||||||
marker = item.get_marker(searched_marker)
|
|
||||||
if marker and condition:
|
|
||||||
assert not marker.args
|
|
||||||
assert set(marker.kwargs).issubset({'reason'})
|
|
||||||
if 'reason' in marker.kwargs:
|
|
||||||
reason = '{}: {}'.format(default_reason,
|
|
||||||
marker.kwargs['reason'])
|
|
||||||
else:
|
|
||||||
reason = default_reason + '.'
|
|
||||||
pytest.skip(reason)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope='session')
|
@pytest.fixture(scope='session')
|
||||||
|
@ -818,7 +818,7 @@ class TestSendOrListen:
|
|||||||
]
|
]
|
||||||
assert records[0].msg == '\n'.join(error_msgs)
|
assert records[0].msg == '\n'.join(error_msgs)
|
||||||
|
|
||||||
@pytest.mark.posix("Flaky on Windows")
|
@pytest.mark.posix(reason="Flaky on Windows")
|
||||||
def test_error_while_listening(self, qlocalserver_mock, caplog, args):
|
def test_error_while_listening(self, qlocalserver_mock, caplog, args):
|
||||||
"""Test an error with the first listen call."""
|
"""Test an error with the first listen call."""
|
||||||
qlocalserver_mock().listen.return_value = False
|
qlocalserver_mock().listen.return_value = False
|
||||||
|
Loading…
Reference in New Issue
Block a user