Debian Codegraph @

hypothesis-python/tests/pytest/test_sideeffect_warnings.py

raw
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 pytest_plugins = "pytester"
14
15 TEST_SCRIPT = """
16 def test_noop():
17 pass
18 """
19
20 LAZY_STRATEGY = "integers()"
21
22 SIDEEFFECT_STATEMENT = f"st.{LAZY_STRATEGY}.wrapped_strategy"
23
24 SIDEEFFECT_SCRIPT = f"""
25 from hypothesis import strategies as st
26
27 {SIDEEFFECT_STATEMENT}
28 """
29
30
31 @pytest.mark.skipif(
32 tuple(map(int, pytest.__version__.split(".")[:2])) <= (6, 1),
33 reason="Older pytest don't capture these warnings during runpytest setup",
34 )
35 def test_sideeffect_warning(testdir):
36 testdir.makeconftest(SIDEEFFECT_SCRIPT)
37 script = testdir.makepyfile(TEST_SCRIPT)
38 result = testdir.runpytest_subprocess(script)
39 assert "HypothesisSideeffectWarning" in "\n".join(result.outlines)
40 assert LAZY_STRATEGY in "\n".join(result.outlines)
41
42
43 def test_conftest_sideeffect_pinpoint_error(testdir, monkeypatch):
44 # -Werror is not sufficient since warning is emitted before session start. Additionally, we
45 # don't want to raise errors from other plugins. Due to limited filtering capabilities of
46 # PYTHONWARNINGS/-W ("message is a literal string that the start of the warning message must
47 # contain" and only built-in categories), we must fall back to the actual message text.
48 monkeypatch.setenv("PYTHONWARNINGS", "error:Slow code in plugin")
49 testdir.makeconftest(SIDEEFFECT_SCRIPT)
50 script = testdir.makepyfile(TEST_SCRIPT)
51 result = testdir.runpytest_subprocess(script)
52 assert "HypothesisSideeffectWarning" in "\n".join(result.errlines)
53 # Plugin is always loaded before conftest, so "during pytest plugin initialization"
54 assert "during pytest" in "\n".join(result.errlines)
55 assert SIDEEFFECT_STATEMENT in "\n".join(result.errlines)
56
57
58 def test_plugin_sideeffect_pinpoint_error(testdir, monkeypatch):
59 # See comment above regarding this line
60 monkeypatch.setenv("PYTHONWARNINGS", "error:Slow code in plugin")
61 # Ensure we see the correct stacktrace regardless of plugin load order
62 monkeypatch.setenv("HYPOTHESIS_EXTEND_INITIALIZATION", "1")
63 testdir.makepyfile(sideeffect_plugin=SIDEEFFECT_SCRIPT)
64 script = testdir.makepyfile(TEST_SCRIPT)
65 result = testdir.runpytest_subprocess(script, "-p", "sideeffect_plugin")
66 assert "HypothesisSideeffectWarning" in "\n".join(result.errlines)
67 # Plugin order unknown, but certainly not at import time
68 assert "at import time" not in "\n".join(result.errlines)
69 assert SIDEEFFECT_STATEMENT in "\n".join(result.errlines)