|
20 | 20 |
|
21 | 21 | IN_INITIALIZATION_ATTR = "in_initialization"
|
22 | 22 |
|
| 23 | +# These tests use the pytest plugin enabling infrastructure to restart the side-effect warnings, |
| 24 | +# rather than trying to induce side-effects during import (and entrypoint loading) itself, which is |
| 25 | +# hard to do. Manual verification of behaviour during initial import can be done by just injecting |
| 26 | +# one of the side-effect-inducing statements below directly into hypothesis.entry_points.run(). |
| 27 | +# Manual validation can also be done by inspecting the relevant state during import and verify that |
| 28 | +# it is the same as tested here |
| 29 | +# (_hypothesis_globals.in_initialization > 0, hypothesis.configuration._first_postinit_what is None) |
| 30 | + |
23 | 31 |
|
24 | 32 | @pytest.fixture
|
25 | 33 | def extend_initialization(monkeypatch):
|
| 34 | + assert getattr(_hypothesis_globals, IN_INITIALIZATION_ATTR) == 0 |
26 | 35 | monkeypatch.setattr(_hypothesis_globals, IN_INITIALIZATION_ATTR, 1)
|
27 | 36 | fs.notice_initialization_restarted(warn=False)
|
| 37 | + assert fs._first_postinit_what is None # validates state as given in comment above |
28 | 38 |
|
29 | 39 |
|
30 | 40 | @pytest.mark.parametrize(
|
31 |
| - "sideeffect_script, warning_text", |
| 41 | + "sideeffect, warning_text", |
32 | 42 | [
|
33 |
| - ("st.integers().is_empty", "lazy evaluation"), |
34 |
| - ("st.deferred(st.integers).is_empty", "deferred evaluation"), |
35 |
| - ("fs.storage_directory()", "accessing storage"), |
| 43 | + (lambda: st.integers().wrapped_strategy, "lazy evaluation"), |
| 44 | + (lambda: st.deferred(st.integers).wrapped_strategy, "deferred evaluation"), |
| 45 | + (fs.storage_directory, "accessing storage"), |
36 | 46 | ],
|
37 | 47 | )
|
38 |
| -def test_sideeffect_warning(sideeffect_script, warning_text, extend_initialization): |
| 48 | +def test_sideeffect_warning(sideeffect, warning_text, extend_initialization): |
39 | 49 | with pytest.warns(HypothesisSideeffectWarning, match=warning_text):
|
40 |
| - exec(sideeffect_script) |
| 50 | + sideeffect() |
41 | 51 |
|
42 | 52 |
|
43 | 53 | def test_sideeffect_delayed_warning(monkeypatch, extend_initialization):
|
|
0 commit comments