Move end2end tag handling to end2end/conftest.py

This commit is contained in:
Florian Bruhin 2016-09-05 10:54:09 +02:00
parent 6c6e98aac4
commit 30327b2acf
3 changed files with 100 additions and 99 deletions

View File

@ -21,11 +21,9 @@
"""The qutebrowser test suite conftest file.""" """The qutebrowser test suite conftest file."""
import re
import os import os
import sys import sys
import warnings import warnings
import operator
import pytest import pytest
import hypothesis import hypothesis
@ -37,10 +35,6 @@ from helpers.logfail import fail_on_logging
from helpers.messagemock import message_mock from helpers.messagemock import message_mock
from helpers.fixtures import * # pylint: disable=wildcard-import from helpers.fixtures import * # pylint: disable=wildcard-import
from PyQt5.QtCore import PYQT_VERSION
from qutebrowser.utils import qtutils
# Set hypothesis settings # Set hypothesis settings
hypothesis.settings.register_profile('default', hypothesis.settings.register_profile('default',
@ -182,72 +176,3 @@ def pytest_sessionfinish(exitstatus):
status_file = os.path.join(cache_dir, 'pytest_status') status_file = os.path.join(cache_dir, 'pytest_status')
with open(status_file, 'w', encoding='ascii') as f: with open(status_file, 'w', encoding='ascii') as f:
f.write(str(exitstatus)) f.write(str(exitstatus))
if not getattr(sys, 'frozen', False):
def _get_version_tag(tag):
"""Handle tags like pyqt>=5.3.1 for BDD tests.
This transforms e.g. pyqt>=5.3.1 into an appropriate @pytest.mark.skip
marker, and falls back to pytest-bdd's implementation for all other
casesinto an appropriate @pytest.mark.skip marker, and falls back to
"""
version_re = re.compile(r"""
(?P<package>qt|pyqt)
(?P<operator>==|>|>=|<|<=|!=)
(?P<version>\d+\.\d+\.\d+)
""", re.VERBOSE)
match = version_re.match(tag)
if not match:
return None
operators = {
'==': operator.eq,
'>': operator.gt,
'<': operator.lt,
'>=': operator.ge,
'<=': operator.le,
'!=': operator.ne,
}
package = match.group('package')
op = operators[match.group('operator')]
version = match.group('version')
if package == 'qt':
return pytest.mark.skipif(qtutils.version_check(version, op),
reason='Needs ' + tag)
elif package == 'pyqt':
major, minor, patch = [int(e) for e in version.split('.')]
hex_version = (major << 16) | (minor << 8) | patch
return pytest.mark.skipif(not op(PYQT_VERSION, hex_version),
reason='Needs ' + tag)
else:
raise ValueError("Invalid package {!r}".format(package))
def _get_backend_tag(tag):
"""Handle a @qtwebengine_*/@qtwebkit_skip tag."""
pytest_marks = {
'qtwebengine_todo': pytest.mark.qtwebengine_todo,
'qtwebengine_skip': pytest.mark.qtwebengine_skip,
'qtwebkit_skip': pytest.mark.qtwebkit_skip
}
if not any(tag.startswith(t + ':') for t in pytest_marks):
return None
name, desc = tag.split(':', maxsplit=1)
return pytest_marks[name](desc)
def pytest_bdd_apply_tag(tag, function):
"""Handle custom tags for BDD tests.
This tries various functions, and if none knows how to handle this tag,
it returns None so it falls back to pytest-bdd's implementation.
"""
funcs = [_get_version_tag, _get_backend_tag]
for func in funcs:
mark = func(tag)
if mark is not None:
mark(function)
return True
return None

View File

@ -21,11 +21,16 @@
"""Things needed for end2end testing.""" """Things needed for end2end testing."""
import re
import os import os
import sys
import shutil import shutil
import pstats import pstats
import os.path import os.path
import operator
import pytest import pytest
from PyQt5.QtCore import PYQT_VERSION
pytest.register_assert_rewrite('end2end.fixtures') pytest.register_assert_rewrite('end2end.fixtures')
@ -33,6 +38,7 @@ from end2end.fixtures.webserver import httpbin, httpbin_after_test, ssl_server
from end2end.fixtures.quteprocess import (quteproc_process, quteproc, from end2end.fixtures.quteprocess import (quteproc_process, quteproc,
quteproc_new) quteproc_new)
from end2end.fixtures.testprocess import pytest_runtest_makereport from end2end.fixtures.testprocess import pytest_runtest_makereport
from qutebrowser.utils import qtutils
def pytest_configure(config): def pytest_configure(config):
@ -51,3 +57,97 @@ def pytest_unconfigure(config):
for fn in os.listdir('prof'): for fn in os.listdir('prof'):
stats.add(os.path.join('prof', fn)) stats.add(os.path.join('prof', fn))
stats.dump_stats(os.path.join('prof', 'combined.pstats')) stats.dump_stats(os.path.join('prof', 'combined.pstats'))
def _get_version_tag(tag):
"""Handle tags like pyqt>=5.3.1 for BDD tests.
This transforms e.g. pyqt>=5.3.1 into an appropriate @pytest.mark.skip
marker, and falls back to pytest-bdd's implementation for all other
casesinto an appropriate @pytest.mark.skip marker, and falls back to
"""
version_re = re.compile(r"""
(?P<package>qt|pyqt)
(?P<operator>==|>|>=|<|<=|!=)
(?P<version>\d+\.\d+\.\d+)
""", re.VERBOSE)
match = version_re.match(tag)
if not match:
return None
operators = {
'==': operator.eq,
'>': operator.gt,
'<': operator.lt,
'>=': operator.ge,
'<=': operator.le,
'!=': operator.ne,
}
package = match.group('package')
op = operators[match.group('operator')]
version = match.group('version')
if package == 'qt':
return pytest.mark.skipif(qtutils.version_check(version, op),
reason='Needs ' + tag)
elif package == 'pyqt':
major, minor, patch = [int(e) for e in version.split('.')]
hex_version = (major << 16) | (minor << 8) | patch
return pytest.mark.skipif(not op(PYQT_VERSION, hex_version),
reason='Needs ' + tag)
else:
raise ValueError("Invalid package {!r}".format(package))
def _get_backend_tag(tag):
"""Handle a @qtwebengine_*/@qtwebkit_skip tag."""
pytest_marks = {
'qtwebengine_todo': pytest.mark.qtwebengine_todo,
'qtwebengine_skip': pytest.mark.qtwebengine_skip,
'qtwebkit_skip': pytest.mark.qtwebkit_skip
}
if not any(tag.startswith(t + ':') for t in pytest_marks):
return None
name, desc = tag.split(':', maxsplit=1)
return pytest_marks[name](desc)
if not getattr(sys, 'frozen', False):
def pytest_bdd_apply_tag(tag, function):
"""Handle custom tags for BDD tests.
This tries various functions, and if none knows how to handle this tag,
it returns None so it falls back to pytest-bdd's implementation.
"""
funcs = [_get_version_tag, _get_backend_tag]
for func in funcs:
mark = func(tag)
if mark is not None:
mark(function)
return True
return None
def pytest_collection_modifyitems(config, items):
"""Apply @qtwebengine_* markers."""
webengine = config.getoption('--qute-bdd-webengine')
markers = {
'qtwebengine_todo': ('QtWebEngine TODO', pytest.mark.xfail, webengine),
'qtwebengine_skip': ('Skipped with QtWebEngine', pytest.mark.skipif,
webengine),
'qtwebkit_skip': ('Skipped with QtWebKit', pytest.mark.skipif,
not webengine),
}
for item in items:
for name, (prefix, pytest_mark, condition) in markers.items():
marker = item.get_marker(name)
if marker:
if marker.args:
text = '{}: {}'.format(prefix, marker.args[0])
else:
text = prefix
item.add_marker(pytest_mark(condition, reason=text,
**marker.kwargs))

View File

@ -34,30 +34,6 @@ from qutebrowser.utils import log
from helpers import utils from helpers import utils
def pytest_collection_modifyitems(config, items):
"""Apply @qtwebengine_* markers."""
webengine = config.getoption('--qute-bdd-webengine')
markers = {
'qtwebengine_todo': ('QtWebEngine TODO', pytest.mark.xfail, webengine),
'qtwebengine_skip': ('Skipped with QtWebEngine', pytest.mark.skipif,
webengine),
'qtwebkit_skip': ('Skipped with QtWebKit', pytest.mark.skipif,
not webengine),
}
for item in items:
for name, (prefix, pytest_mark, condition) in markers.items():
marker = item.get_marker(name)
if marker:
if marker.args:
text = '{}: {}'.format(prefix, marker.args[0])
else:
text = prefix
item.add_marker(pytest_mark(condition, reason=text,
**marker.kwargs))
@pytest.hookimpl(hookwrapper=True) @pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call): def pytest_runtest_makereport(item, call):
"""Add a BDD section to the test output.""" """Add a BDD section to the test output."""