Skip to content

Commit 4fbd0a8

Browse files
committed
Make compatible with pytest-asyncio
`pytest-asyncio` reads [1] `collector.obj` during test collection which raises an `ImportError` for `DocTestTextfilePlus`. We fix this by aligning that class with pytest’s `DoctestTextfile` [2] and setting `obj` to `None`. Fixes #256 [1]: https://github.com/pytest-dev/pytest-asyncio/blob/eb63d5ad0ca21041726ada3d5c00211d36418a9b/pytest_asyncio/plugin.py#L640 [2]: https://github.com/pytest-dev/pytest/blob/fc56ae365fcdea800f91683186136a8191e22399/src/_pytest/doctest.py#L421
1 parent 9a4069f commit 4fbd0a8

File tree

4 files changed

+15
-0
lines changed

4 files changed

+15
-0
lines changed

.github/workflows/python-tests.yml

+3
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ jobs:
7070
- os: ubuntu-latest
7171
python-version: '3.13'
7272
toxenv: py313-test-pytestdev-numpydev
73+
- os: ubuntu-latest
74+
python-version: '3.13'
75+
toxenv: py313-test-pytest83-pytestasyncio
7376

7477
steps:
7578
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

pytest_doctestplus/plugin.py

+1
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ def collect(self):
342342
test.name, self, runner, test)
343343

344344
class DocTestTextfilePlus(pytest.Module):
345+
obj = None
345346

346347
def collect(self):
347348
if PYTEST_GE_7_0:

tests/test_doctestplus.py

+10
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@
1111
import doctest
1212
from pytest_doctestplus.output_checker import OutputChecker, FLOAT_CMP
1313

14+
try:
15+
import pytest_asyncio # noqa: F401
16+
has_pytest_asyncio = True
17+
except ImportError:
18+
has_pytest_asyncio = False
19+
20+
1421

1522
pytest_plugins = ['pytester']
1623

@@ -1123,6 +1130,9 @@ class MyClass:
11231130
assert ("something()\nUNEXPECTED EXCEPTION: NameError" in report.longreprtext) is cont_on_fail
11241131

11251132

1133+
@pytest.mark.xfail(
1134+
has_pytest_asyncio,
1135+
reason='pytest_asyncio monkey-patches .collect()')
11261136
def test_main(testdir):
11271137
pkg = testdir.mkdir('pkg')
11281138
code = dedent(

tox.ini

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ deps =
3232
pytest83: pytest==8.3.*
3333
pytestdev: git+https://github.com/pytest-dev/pytest#egg=pytest
3434
numpydev: numpy>=0.0.dev0
35+
pytestasyncio: pytest-asyncio
3536

3637
extras =
3738
test

0 commit comments

Comments
 (0)