-
Notifications
You must be signed in to change notification settings - Fork 588
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
75 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# This file is part of Hypothesis, which may be found at | ||
# https://github.com/HypothesisWorks/hypothesis/ | ||
# | ||
# Most of this work is copyright (C) 2013-2021 David R. MacIver | ||
# (david@drmaciver.com), but it contains contributions by others. See | ||
# CONTRIBUTING.rst for a full list of people who may hold copyright, and | ||
# consult the git log if you need to determine who owns an individual | ||
# contribution. | ||
# | ||
# This Source Code Form is subject to the terms of the Mozilla Public License, | ||
# v. 2.0. If a copy of the MPL was not distributed with this file, You can | ||
# obtain one at https://mozilla.org/MPL/2.0/. | ||
# | ||
# END HEADER | ||
|
||
import sys | ||
|
||
import pytest | ||
|
||
from hypothesis.internal.compat import PYPY | ||
from hypothesis.internal.scrutineer import make_report | ||
|
||
# We skip tracing for explanations under PyPy, where it has a large performance | ||
# impact, or if there is already a trace function (e.g. coverage or a debugger) | ||
pytestmark = pytest.mark.skipif(PYPY or sys.gettrace(), reason="See comment") | ||
|
||
BUG_MARKER = "# BUG" | ||
PRELUDE = """ | ||
from hypothesis import Phase, given, settings, strategies as st | ||
@settings(phases=tuple(Phase), derandomize=True) | ||
""" | ||
TRIVIAL = """ | ||
@given(st.integers()) | ||
def test_reports_branch_in_test(x): | ||
if x > 10: | ||
raise AssertionError # BUG | ||
""" | ||
MULTIPLE_BUGS = """ | ||
@given(st.integers(), st.integers()) | ||
def test_reports_branch_in_test(x, y): | ||
if x > 10: | ||
raise (AssertionError if x % 2 else Exception) # BUG | ||
""" | ||
FRAGMENTS = ( | ||
pytest.param(TRIVIAL, id="trivial"), | ||
pytest.param(MULTIPLE_BUGS, id="multiple-bugs"), | ||
) | ||
|
||
|
||
def get_reports(file_contents, *, testdir): | ||
# Takes the source code string with "# BUG" comments, and returns a list of | ||
# multi-line report strings which we expect to see in explain-mode output. | ||
# The list length is the number of explainable bugs, usually one. | ||
test_file = str(testdir.makepyfile(file_contents)) | ||
pytest_stdout = str(testdir.runpytest_inprocess(test_file, "--tb=native").stdout) | ||
|
||
explanations = { | ||
i: {(test_file, i)} | ||
for i, line in enumerate(file_contents.splitlines()) | ||
if line.endswith(BUG_MARKER) | ||
} | ||
expected = ["\n".join(r) for k, r in make_report(explanations).items()] | ||
return pytest_stdout, expected | ||
|
||
|
||
@pytest.mark.parametrize("code", FRAGMENTS) | ||
def test_explanations(code, testdir): | ||
pytest_stdout, expected = get_reports(PRELUDE + code, testdir=testdir) | ||
assert len(expected) == code.count(BUG_MARKER) | ||
for report in expected: | ||
assert report in pytest_stdout |