summaryrefslogtreecommitdiff
path: root/tests/conftest.py
blob: 11d7aeceaa72903dfbc04437c0e63536ee7f8627 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
# For details: https://github.com/nedbat/coveragepy/blob/master/NOTICE.txt

"""
Pytest auto configuration.

This module is run automatically by pytest, to define and enable fixtures.
"""

import os
import sys
import warnings

import pytest

from coverage import env
from coverage.misc import StopEverything


# Pytest will rewrite assertions in test modules, but not elsewhere.
# This tells pytest to also rewrite assertions in coveragetest.py.
pytest.register_assert_rewrite("tests.coveragetest")
pytest.register_assert_rewrite("tests.helpers")

# Pytest can take additional options:
# $set_env.py: PYTEST_ADDOPTS - Extra arguments to pytest.

@pytest.fixture(autouse=True)
def set_warnings():
    """Enable DeprecationWarnings during all tests."""
    warnings.simplefilter("default")
    warnings.simplefilter("once", DeprecationWarning)

    # Warnings to suppress:
    # How come these warnings are successfully suppressed here, but not in setup.cfg??

    #   setuptools/py33compat.py:54: DeprecationWarning: The value of convert_charrefs will become
    #   True in 3.5. You are encouraged to set the value explicitly.
    #       unescape = getattr(html, 'unescape', html_parser.HTMLParser().unescape)
    warnings.filterwarnings(
        "ignore",
        category=DeprecationWarning,
        message=r"The value of convert_charrefs will become True in 3.5.",
        )

    warnings.filterwarnings(
        "ignore",
        category=DeprecationWarning,
        message=r".* instead of inspect.getfullargspec",
        )

    # <frozen importlib._bootstrap>:681:
    # ImportWarning: VendorImporter.exec_module() not found; falling back to load_module()
    warnings.filterwarnings(
        "ignore",
        category=ImportWarning,
        message=r".*exec_module\(\) not found; falling back to load_module\(\)",
        )

    if env.PYPY3:
        # pypy3 warns about unclosed files a lot.
        warnings.filterwarnings("ignore", r".*unclosed file", category=ResourceWarning)


@pytest.fixture(autouse=True)
def reset_sys_path():
    """Clean up sys.path changes around every test."""
    sys_path = list(sys.path)
    yield
    sys.path[:] = sys_path


@pytest.fixture(autouse=True)
def fix_xdist_sys_path():
    """Prevent xdist from polluting the Python path.

    We run tests that care a lot about the contents of sys.path.  Pytest-xdist
    changes sys.path, so running with xdist, vs without xdist, sets sys.path
    differently.  With xdist, sys.path[1] is an empty string, without xdist,
    it's the virtualenv bin directory.  We don't want the empty string, so
    clobber that entry.

    See: https://github.com/pytest-dev/pytest-xdist/issues/376

    """
    if os.environ.get('PYTEST_XDIST_WORKER', ''):       # pragma: part covered
        # We are running in an xdist worker.
        if sys.path[1] == '':
            # xdist has set sys.path[1] to ''.  Clobber it.
            del sys.path[1]
        # Also, don't let it sneak stuff in via PYTHONPATH.
        try:
            del os.environ['PYTHONPATH']
        except KeyError:
            pass


@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_call(item):
    """Convert StopEverything into skipped tests."""
    outcome = yield
    if outcome.excinfo and issubclass(outcome.excinfo[0], StopEverything):
        pytest.skip("Skipping {} for StopEverything: {}".format(item.nodeid, outcome.excinfo[1]))