| 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 pytest |
| 12 |
|
| 13 |
from hypothesis._settings import _CI_VARS |
| 14 |
from hypothesis.internal.compat import WINDOWS, escape_unicode_characters |
| 15 |
|
| 16 |
pytest_plugins = "pytester" |
| 17 |
|
| 18 |
TESTSUITE = """ |
| 19 |
from hypothesis import given, settings, Verbosity |
| 20 |
from hypothesis.strategies import integers |
| 21 |
|
| 22 |
@settings(verbosity=Verbosity.verbose) |
| 23 |
@given(integers()) |
| 24 |
def test_should_be_verbose(x): |
| 25 |
pass |
| 26 |
|
| 27 |
""" |
| 28 |
|
| 29 |
|
| 30 |
@pytest.mark.parametrize("capture,expected", [("no", True), ("fd", False)]) |
| 31 |
def test_output_without_capture(testdir, capture, expected): |
| 32 |
script = testdir.makepyfile(TESTSUITE) |
| 33 |
result = testdir.runpytest(script, "--verbose", "--capture", capture) |
| 34 |
out = "\n".join(result.stdout.lines) |
| 35 |
assert "test_should_be_verbose" in out |
| 36 |
assert ("Trying example" in out) == expected |
| 37 |
assert result.ret == 0 |
| 38 |
|
| 39 |
|
| 40 |
UNICODE_EMITTING = """ |
| 41 |
import pytest |
| 42 |
from hypothesis import given, settings, Verbosity |
| 43 |
from hypothesis.strategies import text |
| 44 |
import sys |
| 45 |
|
| 46 |
def test_emits_unicode(): |
| 47 |
@settings(verbosity=Verbosity.verbose) |
| 48 |
@given(text()) |
| 49 |
def test_should_emit_unicode(t): |
| 50 |
assert all(ord(c) <= 1000 for c in t), ascii(t) |
| 51 |
with pytest.raises(AssertionError): |
| 52 |
test_should_emit_unicode() |
| 53 |
""" |
| 54 |
|
| 55 |
|
| 56 |
@pytest.mark.xfail( |
| 57 |
WINDOWS, |
| 58 |
reason="Encoding issues in running the subprocess, possibly pytest's fault", |
| 59 |
strict=False, # It's possible, if rare, for this to pass on Windows too. |
| 60 |
) |
| 61 |
def test_output_emitting_unicode(testdir, monkeypatch): |
| 62 |
monkeypatch.setenv("LC_ALL", "C") |
| 63 |
monkeypatch.setenv("LANG", "C") |
| 64 |
script = testdir.makepyfile(UNICODE_EMITTING) |
| 65 |
result = getattr(testdir, "runpytest_subprocess", testdir.runpytest)( |
| 66 |
script, "--verbose", "--capture=no" |
| 67 |
) |
| 68 |
out = "\n".join(result.stdout.lines) |
| 69 |
assert "test_emits_unicode" in out |
| 70 |
assert chr(1001) in out or escape_unicode_characters(chr(1001)) in out |
| 71 |
assert result.ret == 0 |
| 72 |
|
| 73 |
|
| 74 |
def get_line_num(token, lines, skip_n=0): |
| 75 |
skipped = 0 |
| 76 |
for i, line in enumerate(lines): |
| 77 |
if token in line: |
| 78 |
if skip_n == skipped: |
| 79 |
return i |
| 80 |
else: |
| 81 |
skipped += 1 |
| 82 |
raise AssertionError( |
| 83 |
f"Token {token!r} not found (skipped {skipped} of planned {skip_n} skips)" |
| 84 |
) |
| 85 |
|
| 86 |
|
| 87 |
TRACEBACKHIDE_HEALTHCHECK = """ |
| 88 |
from hypothesis import given, settings |
| 89 |
from hypothesis.strategies import integers |
| 90 |
import time |
| 91 |
@given(integers().map(lambda x: time.sleep(0.2))) |
| 92 |
def test_healthcheck_traceback_is_hidden(x): |
| 93 |
pass |
| 94 |
""" |
| 95 |
|
| 96 |
|
| 97 |
def test_healthcheck_traceback_is_hidden(testdir, monkeypatch): |
| 98 |
for key in _CI_VARS: |
| 99 |
monkeypatch.delenv(key, raising=False) |
| 100 |
|
| 101 |
script = testdir.makepyfile(TRACEBACKHIDE_HEALTHCHECK) |
| 102 |
lines = testdir.runpytest(script, "--verbose").stdout.lines |
| 103 |
def_token = "__ test_healthcheck_traceback_is_hidden __" |
| 104 |
timeout_token = ": FailedHealthCheck" |
| 105 |
def_line = get_line_num(def_token, lines) |
| 106 |
timeout_line = get_line_num(timeout_token, lines) |
| 107 |
# 16 on pytest 8.4.0 combined with py3{9, 10} or 3.13 free-threading (but |
| 108 |
# not with 3.13 normal??) |
| 109 |
assert timeout_line - def_line in {15, 16} |
| 110 |
|
| 111 |
|
| 112 |
COMPOSITE_IS_NOT_A_TEST = """ |
| 113 |
from hypothesis.strategies import composite, none |
| 114 |
@composite |
| 115 |
def test_data_factory(draw): |
| 116 |
return draw(none()) |
| 117 |
""" |
| 118 |
|
| 119 |
|
| 120 |
def test_deprecation_of_strategies_as_tests(testdir): |
| 121 |
script = testdir.makepyfile(COMPOSITE_IS_NOT_A_TEST) |
| 122 |
testdir.runpytest(script).assert_outcomes(failed=1) |