Skip to content

Commit

Permalink
Adapt to Node API change
Browse files Browse the repository at this point in the history
The Node API was changed in pytest 5.4 in
pytest-dev/pytest#5975.

This commit adds support for the new API but maintains backwards
compatiblity.

Closes: #11
  • Loading branch information
twmr committed Apr 1, 2020
1 parent e72d593 commit 8d38df8
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions pytest_sphinx.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@
import sys
import traceback

from pkg_resources import parse_version

import _pytest.doctest
import pytest


PYTEST_PRE_54 = parse_version(pytest.__version__) < parse_version('5.4')


def pairwise(iterable):
"""
s -> (s0,s1), (s1,s2), (s2, s3), ...
Expand All @@ -41,9 +46,15 @@ def pytest_collect_file(path, parent):
config = parent.config
if path.ext == ".py":
if config.option.doctestmodules:
return SphinxDoctestModule(path, parent)
if PYTEST_PRE_54:
return SphinxDoctestModule(path, parent)
else:
return SphinxDoctestModule.from_parent(parent, fspath=path)
elif _is_doctest(config, path, parent):
return SphinxDoctestTextfile(path, parent)
if PYTEST_PRE_54:
return SphinxDoctestTextfile(path, parent)
else:
return SphinxDoctestTextfile.from_parent(parent, fspath=path)


def _is_doctest(config, path, parent):
Expand Down Expand Up @@ -343,8 +354,11 @@ def collect(self):
docstring=text)

if test.examples:
yield _pytest.doctest.DoctestItem(
test.name, self, runner, test)
if PYTEST_PRE_54:
yield _pytest.doctest.DoctestItem(test.name, self, runner, test)
else:
yield _pytest.doctest.DoctestItem.from_parent(
parent=self, name=test.name, runner=runner, dtest=test)


class SphinxDoctestModule(pytest.Module):
Expand Down Expand Up @@ -395,5 +409,9 @@ def _find(self, tests, obj, name, module, source_lines,

for test in finder.find(module, module.__name__):
if test.examples:
yield _pytest.doctest.DoctestItem(
test.name, self, runner, test)
if PYTEST_PRE_54:
yield _pytest.doctest.DoctestItem(
test.name, self, runner, test)
else:
yield _pytest.doctest.DoctestItem.from_parent(
parent=self, name=test.name, runner=runner, dtest=test)

0 comments on commit 8d38df8

Please sign in to comment.