Skip to content

Commit e001635

Browse files
committed
Conditionally define the pytest doctest target
1 parent 6b6ca2c commit e001635

File tree

1 file changed

+37
-19
lines changed

1 file changed

+37
-19
lines changed

src/flint/test/test_docstrings.py

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import doctest
22
import importlib
33
import pkgutil
4-
import pytest
54
import re
65

76
import flint
@@ -32,24 +31,43 @@ def find_doctests(module):
3231
return tests
3332

3433

35-
class PyTestDocTestRunner(doctest.DocTestRunner):
36-
def report_failure(self, out, test, example, got):
37-
pytest.fail(
38-
"\n".join([
39-
f"{test.name}, line: {test.lineno}",
40-
"Failed example:",
41-
f"\t{example.source.strip()}",
42-
"Expected:",
43-
f"\t{example.want.strip()}",
44-
"Got:",
45-
f"\t{got.strip()}"
46-
]),
47-
pytrace=False,
48-
)
34+
# The below definitions are only useful when pytest is a) installed, and b) being currently run.
35+
# We don't want to impose pytest on those that just want to use `python -m flint.test`
36+
try:
37+
import pytest
4938

39+
class PyTestDocTestRunner(doctest.DocTestRunner):
40+
def report_failure(self, out, test, example, got):
41+
pytest.fail(
42+
"\n".join([
43+
f"{test.name}, line: {test.lineno}",
44+
"Failed example:",
45+
f"\t{example.source.strip()}",
46+
"Expected:",
47+
f"\t{example.want.strip()}",
48+
"Got:",
49+
f"\t{got.strip()}"
50+
]),
51+
pytrace=False,
52+
)
5053

51-
runner = PyTestDocTestRunner()
54+
runner = PyTestDocTestRunner()
5255

53-
@pytest.mark.parametrize("module,test", [(module, test) for module, test_set in find_doctests(flint) for test in test_set])
54-
def test_docstrings(module, test):
55-
runner.run(test)
56+
@pytest.mark.parametrize(
57+
"test",
58+
[
59+
test for _, test_set in find_doctests(flint)
60+
for test in test_set
61+
]
62+
)
63+
def test_docstrings(test):
64+
runner.run(test)
65+
66+
except ImportError:
67+
class PyTestDocTestRunner(doctest.DocTestRunner):
68+
pass
69+
70+
runner = None
71+
72+
def test_docstrings(module, test):
73+
pass

0 commit comments

Comments
 (0)