Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use from_parent() constructor from pytest 5.4 #47

Merged
merged 2 commits into from
May 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ language: python

jobs:
include:
- env: TOXENV=py27
- env: TOXENV=py27-pytestlatest
python: '2.7'
- env: TOXENV=py35
- env: TOXENV=py35-pytestlatest
python: '3.5'
- env: TOXENV=py36
- env: TOXENV=py36-pytestlatest
python: '3.6'
- env: TOXENV=py37
- env: TOXENV=py37-pytestlatest
python: '3.7'
- env: TOXENV=py38
- env: TOXENV=py38-pytestlatest
python: '3.8'
- env: TOXENV=py38-pytest53
python: '3.8'
- stage: deploy
python: '3.8'
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 1.2.1

- Remove `from_parent()` warnings in pytest 5.4.2+.

# 1.2.0

- `pytest-cpp` no longer supports Python 3.4.
Expand Down
32 changes: 26 additions & 6 deletions pytest_cpp/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
_ARGUMENTS = 'cpp_arguments'


# pytest 5.4 introduced the 'from_parent' constructor
needs_from_parent = hasattr(pytest.Item, "from_parent")


def pytest_collect_file(parent, path):
try:
is_executable = os.stat(str(path)).st_mode & stat.S_IXUSR
Expand All @@ -38,7 +42,10 @@ def pytest_collect_file(parent, path):
return
for facade_class in FACADES:
if facade_class.is_test_suite(str(path)):
return CppFile(path, parent, facade_class(), test_args)
if needs_from_parent:
return CppFile.from_parent(fspath=path, parent=parent, facade=facade_class(), arguments=test_args)
else:
return CppFile(path, parent, facade_class(), test_args)


def pytest_addoption(parser):
Expand All @@ -57,22 +64,35 @@ def pytest_addoption(parser):


class CppFile(pytest.File):
def __init__(self, path, parent, facade, arguments):
pytest.File.__init__(self, path, parent)
def __init__(self, fspath, parent, facade, arguments):
pytest.File.__init__(self, fspath, parent)
self.facade = facade
self._arguments = arguments

@classmethod
def from_parent(cls, parent, fspath, facade, arguments):
# TODO: after dropping python 2, change to keyword only after 'parent'
return super().from_parent(parent=parent, fspath=fspath, facade=facade, arguments=arguments)

def collect(self):
for test_id in self.facade.list_tests(str(self.fspath)):
yield CppItem(test_id, self, self.facade, self._arguments)
if needs_from_parent:
yield CppItem.from_parent(parent=self, name=test_id, facade=self.facade, arguments=self._arguments)
else:
yield CppItem(test_id, self, self.facade, self._arguments)


class CppItem(pytest.Item):
def __init__(self, name, collector, facade, arguments):
pytest.Item.__init__(self, name, collector)
def __init__(self, name, parent, facade, arguments):
pytest.Item.__init__(self, name, parent)
self.facade = facade
self._arguments = arguments

@classmethod
def from_parent(cls, parent, name, facade, arguments):
# TODO: after dropping python 2, change to keyword only after 'parent'
return super().from_parent(name=name, parent=parent, facade=facade, arguments=arguments)

def runtest(self):
failures = self.facade.run_test(str(self.fspath),
self.name,
Expand Down
5 changes: 4 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
entry_points={
'pytest11': ['cpp = pytest_cpp.plugin'],
},
install_requires=['pytest', 'colorama'],
install_requires=[
'pytest !=5.4.0, !=5.4.1',
'colorama',
],

# metadata for upload to PyPI
author="Bruno Oliveira",
Expand Down
7 changes: 4 additions & 3 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
[tox]
envlist = py{27,35,36,37,38}
envlist = py{27,35,36,37,38,38}-pytestlatest,py38-pytest53

[testenv]
deps=
pytest
pytestlatest: pytest
pytest53: pytest ~=5.3
pytest-mock
pytest-xdist
coverage
commands=
coverage run --source=pytest_cpp -m pytest tests
py.test -n8 tests
pytest -n8 tests