forked from scientific-python/pytest-doctestplus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
65 lines (49 loc) · 1.95 KB
/
conftest.py
File metadata and controls
65 lines (49 loc) · 1.95 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
61
62
63
64
65
import sys
import textwrap
from functools import partial
from packaging.version import Version
from platform import python_version
import pytest
import numpy as np
PYTEST_LT_8_5 = Version(pytest.__version__) < Version('8.5.0.dev')
# Keep this until we require numpy to be >=2.0 or there is a directive in doctestplus
# to support multiple ways of repr
if Version(np.__version__) >= Version("2.0.dev"):
np.set_printoptions(legacy="1.25")
def _wrap_docstring_in_func(func_name, docstring):
template = textwrap.dedent(r"""
def {}():
r'''
{}
'''
""")
return template.format(func_name, docstring)
@pytest.fixture
def makepyfile(testdir):
"""Fixture for making python files with single function and docstring."""
def make(*args, **kwargs):
func_name = kwargs.pop('func_name', 'f')
# content in args and kwargs is treated as docstring
wrap = partial(_wrap_docstring_in_func, func_name)
args = map(wrap, args)
kwargs = dict(zip(kwargs.keys(), map(wrap, kwargs.values())))
return testdir.makepyfile(*args, **kwargs)
return make
@pytest.fixture
def maketestfile(makepyfile):
"""Fixture for making python test files with single function and docstring."""
def make(*args, **kwargs):
func_name = kwargs.pop('func_name', 'test_foo')
return makepyfile(*args, func_name=func_name, **kwargs)
return make
@pytest.fixture
def makerstfile(testdir):
"""Fixture for making rst files with specified content."""
def make(*args, **kwargs):
return testdir.makefile('.rst', *args, **kwargs)
return make
# Windows + Python 3.14.0 + pytest-dev have ResourceWarning, see
# https://github.com/scientific-python/pytest-doctestplus/issues/305
def pytest_runtestloop(session):
if sys.platform == 'win32' and python_version() == "3.14.0" and not PYTEST_LT_8_5:
session.add_marker(pytest.mark.filterwarnings('ignore::ResourceWarning'))