This repository was archived by the owner on Mar 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_example.py
More file actions
60 lines (44 loc) · 1.61 KB
/
Copy pathtest_example.py
File metadata and controls
60 lines (44 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import pytest
from hypothesis import given, settings, HealthCheck
from hypothesis.strategies import text
pytest_plugins = ["pytester"]
@pytest.fixture(scope='module')
def thing():
return object()
@given(s=text())
def test_foo(thing, subtest, s):
outer_thing = thing
@subtest
def test_inner(tmpdir, thing):
# A fresh tmpdir is created for each run of `test_inner`. This is not
# the case if the tmpdir fixture is required in `test_foo`, in that
# case test state would be leaked.
# The `thing` fixture is module-level, and is only set up once.
assert thing is outer_thing
assert not tmpdir.listdir()
tmpdir.join('lol').write(repr(s))
try:
test_foo = settings(suppress_health_check=[HealthCheck.function_scoped_fixture])(test_foo) # type: ignore
except AttributeError:
pass
def test_failure(pytester):
"""Make sure test failures do not crash pytest."""
pytester.makepyfile(
"""
import pytest
from hypothesis import given, settings, HealthCheck
from hypothesis.strategies import text
@pytest.fixture(scope='function')
def thing():
return object()
@given(s=text())
@settings(suppress_health_check=[HealthCheck.function_scoped_fixture])
def test_foo(subtest, s):
@subtest
def test_inner(thing):
assert s, "the string is empty"
"""
)
result = pytester.runpytest()
result.assert_outcomes(failed=1)
result.stdout.fnmatch_lines(["FAILED test_failure.py::test_foo - AssertionError: the string is empty"])