|
| 1 | +from __future__ import print_function |
| 2 | +import pytest |
| 3 | +import os |
| 4 | +import sys |
| 5 | +import subprocess |
| 6 | +import shlex |
| 7 | +import hashlib |
| 8 | +from inspect import getmembers, isfunction |
| 9 | +from allure_commons_test.report import AllureReport |
| 10 | +from allure_commons.utils import thread_tag |
| 11 | + |
| 12 | + |
| 13 | +with open("debug-runner", "w") as debugfile: |
| 14 | + # overwrite debug-runner file with an empty one |
| 15 | + print("New session", file=debugfile) |
| 16 | + |
| 17 | + |
| 18 | +def _get_hash(input): |
| 19 | + if sys.version_info < (3, 0): |
| 20 | + data = bytes(input) |
| 21 | + else: |
| 22 | + data = bytes(input, 'utf8') |
| 23 | + return hashlib.md5(data).hexdigest() |
| 24 | + |
| 25 | + |
| 26 | +@pytest.fixture(scope='function', autouse=True) |
| 27 | +def inject_matchers(doctest_namespace): |
| 28 | + import hamcrest |
| 29 | + for name, function in getmembers(hamcrest, isfunction): |
| 30 | + doctest_namespace[name] = function |
| 31 | + |
| 32 | + from allure_commons_test import container, label, report, result |
| 33 | + for module in [container, label, report, result]: |
| 34 | + for name, function in getmembers(module, isfunction): |
| 35 | + doctest_namespace[name] = function |
| 36 | + |
| 37 | + |
| 38 | +def _runner(allure_dir, module, *extra_params): |
| 39 | + extra_params = ' '.join(extra_params) |
| 40 | + cmd = shlex.split('%s -m pytest --allure-capture --alluredir=%s %s %s' % (sys.executable, allure_dir, extra_params, module), |
| 41 | + posix=False if os.name == "nt" else True) |
| 42 | + with open("debug-runner", "a") as debugfile: |
| 43 | + try: |
| 44 | + subprocess.check_output(cmd, stderr = subprocess.STDOUT) |
| 45 | + except subprocess.CalledProcessError as e: |
| 46 | + # Save to debug file errors on execution (includes pytest failing tests) |
| 47 | + print(e.output, file=debugfile) |
| 48 | + |
| 49 | + |
| 50 | +@pytest.fixture(scope='module') |
| 51 | +def allure_report_with_params(request, tmpdir_factory): |
| 52 | + module = request.module.__file__ |
| 53 | + tmpdir = tmpdir_factory.mktemp('data') |
| 54 | + |
| 55 | + def run_with_params(*params, **kwargs): |
| 56 | + cache = kwargs.get("cache", True) |
| 57 | + key = _get_hash('{thread}{module}{param}'.format(thread=thread_tag(), module=module, param=''.join(params))) |
| 58 | + if not request.config.cache.get(key, False): |
| 59 | + _runner(tmpdir.strpath, module, *params) |
| 60 | + if cache: |
| 61 | + request.config.cache.set(key, True) |
| 62 | + |
| 63 | + def clear_cache(): |
| 64 | + request.config.cache.set(key, False) |
| 65 | + request.addfinalizer(clear_cache) |
| 66 | + |
| 67 | + return AllureReport(tmpdir.strpath) |
| 68 | + return run_with_params |
| 69 | + |
| 70 | + |
| 71 | +@pytest.fixture(scope='module') |
| 72 | +def allure_report(request, tmpdir_factory): |
| 73 | + module = request.module.__file__ |
| 74 | + tmpdir = tmpdir_factory.mktemp('data') |
| 75 | + _runner(tmpdir.strpath, module) |
| 76 | + return AllureReport(tmpdir.strpath) |
| 77 | + |
| 78 | + |
| 79 | +def pytest_collection_modifyitems(items, config): |
| 80 | + if config.option.doctestmodules: |
| 81 | + items[:] = [item for item in items if item.__class__.__name__ == 'DoctestItem'] |
| 82 | + |
| 83 | + |
| 84 | +def pytest_ignore_collect(path, config): |
| 85 | + if sys.version_info.major < 3 and "py3_only" in path.strpath: |
| 86 | + return True |
| 87 | + |
| 88 | + if sys.version_info.major > 2 and "py2_only" in path.strpath: |
| 89 | + return True |
0 commit comments