|
1 | 1 | import doctest |
2 | 2 | import importlib |
3 | 3 | import pkgutil |
4 | | -import pytest |
5 | 4 | import re |
6 | 5 |
|
7 | 6 | import flint |
@@ -32,24 +31,43 @@ def find_doctests(module): |
32 | 31 | return tests |
33 | 32 |
|
34 | 33 |
|
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 |
49 | 38 |
|
| 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 | + ) |
50 | 53 |
|
51 | | -runner = PyTestDocTestRunner() |
| 54 | + runner = PyTestDocTestRunner() |
52 | 55 |
|
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