| 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 |
from random import Random |
| 12 |
|
| 13 |
import pytest |
| 14 |
|
| 15 |
from hypothesis import ( |
| 16 |
HealthCheck, |
| 17 |
Verbosity, |
| 18 |
core, |
| 19 |
given, |
| 20 |
settings, |
| 21 |
strategies as st, |
| 22 |
) |
| 23 |
|
| 24 |
from tests.common.utils import Why, no_shrink, xfail_on_crosshair |
| 25 |
|
| 26 |
|
| 27 |
@pytest.mark.skipif( |
| 28 |
settings.get_current_profile_name() == "crosshair", |
| 29 |
reason="we do not yet pass backends the global random seed, so they are not deterministic", |
| 30 |
) |
| 31 |
def test_seeds_off_internal_random(): |
| 32 |
choices1 = [] |
| 33 |
choices2 = [] |
| 34 |
|
| 35 |
@given(st.integers()) |
| 36 |
def f1(n): |
| 37 |
choices1.append(n) |
| 38 |
|
| 39 |
@given(st.integers()) |
| 40 |
def f2(n): |
| 41 |
choices2.append(n) |
| 42 |
|
| 43 |
core.threadlocal._hypothesis_global_random = Random(0) |
| 44 |
state = core.threadlocal._hypothesis_global_random.getstate() |
| 45 |
f1() |
| 46 |
|
| 47 |
core.threadlocal._hypothesis_global_random.setstate(state) |
| 48 |
f2() |
| 49 |
|
| 50 |
assert choices1 == choices2 |
| 51 |
|
| 52 |
|
| 53 |
@xfail_on_crosshair(Why.nested_given) |
| 54 |
def test_nesting_with_control_passes_health_check(): |
| 55 |
@given(st.integers(0, 100), st.random_module()) |
| 56 |
@settings( |
| 57 |
max_examples=5, |
| 58 |
database=None, |
| 59 |
deadline=None, |
| 60 |
suppress_health_check=[HealthCheck.nested_given], |
| 61 |
) |
| 62 |
def test_blah(x, rnd): |
| 63 |
@given(st.integers()) |
| 64 |
@settings( |
| 65 |
max_examples=100, phases=no_shrink, database=None, verbosity=Verbosity.quiet |
| 66 |
) |
| 67 |
def test_nest(y): |
| 68 |
assert y < x |
| 69 |
|
| 70 |
with pytest.raises(AssertionError): |
| 71 |
test_nest() |
| 72 |
|
| 73 |
test_blah() |