| 1 |
# This file is part of Hypothesis, which may be found at |
| 2 |
# https://github.com/HypothesisWorks/hypothesis/ |
| 3 |
# |
| 4 |
# Copyright the Hypothesis Authors. |
| 5 |
# Individual contributors are listed in AUTHORS.rst and the git log. |
| 6 |
# |
| 7 |
# This Source Code Form is subject to the terms of the Mozilla Public License, |
| 8 |
# v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 9 |
# obtain one at https://mozilla.org/MPL/2.0/. |
| 10 |
|
| 11 |
import json |
| 12 |
import sys |
| 13 |
import sysconfig |
| 14 |
|
| 15 |
import pytest |
| 16 |
|
| 17 |
from hypothesis import given, note, settings, strategies as st |
| 18 |
from hypothesis.internal.compat import PYPY |
| 19 |
from hypothesis.internal.scrutineer import make_report |
| 20 |
from hypothesis.vendor import pretty |
| 21 |
|
| 22 |
from tests.common.utils import skipif_threading |
| 23 |
|
| 24 |
# We skip tracing for explanations under PyPy, where it has a large performance |
| 25 |
# impact, or if there is already a trace function (e.g. coverage or a debugger) |
| 26 |
pytestmark = pytest.mark.skipif(PYPY or sys.gettrace(), reason="See comment") |
| 27 |
|
| 28 |
BUG_MARKER = "# BUG" |
| 29 |
DEADLINE_PRELUDE = """ |
| 30 |
from datetime import timedelta |
| 31 |
from hypothesis.errors import DeadlineExceeded |
| 32 |
""" |
| 33 |
PRELUDE = """ |
| 34 |
from hypothesis import Phase, given, settings, strategies as st |
| 35 |
|
| 36 |
@settings(phases=tuple(Phase), derandomize=True) |
| 37 |
""" |
| 38 |
TRIVIAL = """ |
| 39 |
@given(st.integers()) |
| 40 |
def test_reports_branch_in_test(x): |
| 41 |
if x > 10: |
| 42 |
raise AssertionError # BUG |
| 43 |
""" |
| 44 |
MULTIPLE_BUGS = """ |
| 45 |
@given(st.integers(), st.integers()) |
| 46 |
def test_reports_branch_in_test(x, y): |
| 47 |
if x > 10: |
| 48 |
raise (AssertionError if x % 2 else Exception) # BUG |
| 49 |
""" |
| 50 |
FRAGMENTS = ( |
| 51 |
pytest.param(TRIVIAL, id="trivial"), |
| 52 |
pytest.param(MULTIPLE_BUGS, id="multiple-bugs"), |
| 53 |
) |
| 54 |
|
| 55 |
|
| 56 |
def get_reports(file_contents, *, testdir): |
| 57 |
# Takes the source code string with "# BUG" comments, and returns a list of |
| 58 |
# multi-line report strings which we expect to see in explain-mode output. |
| 59 |
# The list length is the number of explainable bugs, usually one. |
| 60 |
test_file = str(testdir.makepyfile(file_contents)) |
| 61 |
pytest_stdout = str(testdir.runpytest_inprocess(test_file, "--tb=native").stdout) |
| 62 |
|
| 63 |
crash = "AttributeError: module 'blib2to3.pygram' has no attribute 'python_symbols'" |
| 64 |
if crash in pytest_stdout: |
| 65 |
pytest.xfail(reason="upstream error in Black") |
| 66 |
|
| 67 |
explanations = { |
| 68 |
i: {(test_file, i)} |
| 69 |
for i, line in enumerate(file_contents.splitlines()) |
| 70 |
if line.endswith(BUG_MARKER) |
| 71 |
} |
| 72 |
expected = [ |
| 73 |
("\n".join(r), "\n | ".join(r)) # single, ExceptionGroup |
| 74 |
for r in make_report(explanations).values() |
| 75 |
] |
| 76 |
return pytest_stdout, expected |
| 77 |
|
| 78 |
|
| 79 |
@skipif_threading # runpytest_inprocess is not thread safe |
| 80 |
@pytest.mark.parametrize("code", FRAGMENTS) |
| 81 |
def test_explanations(code, testdir): |
| 82 |
pytest_stdout, expected = get_reports(PRELUDE + code, testdir=testdir) |
| 83 |
assert len(expected) == code.count(BUG_MARKER) |
| 84 |
for single, group in expected: |
| 85 |
assert single in pytest_stdout or group in pytest_stdout |
| 86 |
|
| 87 |
|
| 88 |
@skipif_threading # runpytest_inprocess is not thread safe |
| 89 |
@pytest.mark.parametrize("code", FRAGMENTS) |
| 90 |
def test_no_explanations_if_deadline_exceeded(code, testdir): |
| 91 |
code = code.replace("AssertionError", "DeadlineExceeded(timedelta(), timedelta())") |
| 92 |
pytest_stdout, _ = get_reports(DEADLINE_PRELUDE + PRELUDE + code, testdir=testdir) |
| 93 |
assert "Explanation:" not in pytest_stdout |
| 94 |
|
| 95 |
|
| 96 |
NO_SHOW_CONTEXTLIB = """ |
| 97 |
from contextlib import contextmanager |
| 98 |
from hypothesis import given, strategies as st, Phase, settings |
| 99 |
|
| 100 |
@contextmanager |
| 101 |
def ctx(): |
| 102 |
yield |
| 103 |
|
| 104 |
@settings(phases=list(Phase)) |
| 105 |
@given(st.integers()) |
| 106 |
def test(x): |
| 107 |
with ctx(): |
| 108 |
assert x < 100 |
| 109 |
""" |
| 110 |
|
| 111 |
|
| 112 |
@skipif_threading # runpytest_inprocess is not thread safe |
| 113 |
@pytest.mark.skipif(PYPY, reason="Tracing is slow under PyPy") |
| 114 |
def test_skips_uninformative_locations(testdir): |
| 115 |
pytest_stdout, _ = get_reports(NO_SHOW_CONTEXTLIB, testdir=testdir) |
| 116 |
assert "Explanation:" not in pytest_stdout |
| 117 |
|
| 118 |
|
| 119 |
@given(st.randoms()) |
| 120 |
@settings(max_examples=5) |
| 121 |
def test_report_sort(random): |
| 122 |
# show local files first, then site-packages, then stdlib |
| 123 |
|
| 124 |
lines = [ |
| 125 |
# local |
| 126 |
(__file__, 10), |
| 127 |
# site-packages |
| 128 |
(pytest.__file__, 123), |
| 129 |
(pytest.__file__, 124), |
| 130 |
# stdlib |
| 131 |
(json.__file__, 43), |
| 132 |
(json.__file__, 42), |
| 133 |
] |
| 134 |
random.shuffle(lines) |
| 135 |
explanations = {"origin": lines} |
| 136 |
report = make_report(explanations) |
| 137 |
report_lines = report["origin"][2:] |
| 138 |
report_lines = [line.strip() for line in report_lines] |
| 139 |
|
| 140 |
expected_lines = [ |
| 141 |
f"{__file__}:10", |
| 142 |
f"{pytest.__file__}:123", |
| 143 |
f"{pytest.__file__}:124", |
| 144 |
f"{json.__file__}:42", |
| 145 |
f"{json.__file__}:43", |
| 146 |
] |
| 147 |
|
| 148 |
note(f"sysconfig.get_paths(): {pretty.pretty(sysconfig.get_paths())}") |
| 149 |
note(f"actual lines: {pretty.pretty(report_lines)}") |
| 150 |
note(f"expected lines: {pretty.pretty(expected_lines)}") |
| 151 |
|
| 152 |
assert report_lines == expected_lines |