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

Add support for a hook to override skip decision. #77

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def run(self):
),
package_dir = {"": "src"},
python_requires = ">=3.4",
py_modules = ["pytest_dependency"],
py_modules = ["pytest_dependency", "pytest_dependency_hooks"],
install_requires = ["setuptools", "pytest >= 3.7.0"],
entry_points = {
"pytest11": [
Expand Down
16 changes: 16 additions & 0 deletions src/pytest_dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ def addResult(self, rep):
def isSuccess(self):
return list(self.results.values()) == ['passed', 'passed', 'passed']

def hasFailure(self):
return any(outcome == 'failed' for outcome in self.results.values())

class DependencyManager(object):
"""Dependency manager, stores the results of tests.
Expand Down Expand Up @@ -82,16 +84,23 @@ def checkDepend(self, depends, item):
logger.debug("check dependencies of %s in %s scope ...",
item.name, self.scope)
for i in depends:
has_failure = False
if i in self.results:
if self.results[i].isSuccess():
logger.debug("... %s succeeded", i)
continue
else:
logger.debug("... %s has not succeeded", i)
if self.results[i].hasFailure():
has_failure = True
else:
logger.debug("... %s is unknown", i)
if _ignore_unknown:
continue
if not has_failure and any(item.config.hook.pytest_dependency_override_skip(
item=item, dependency=i, scope=self.scope)):
logger.info("NOT skipping %s because its dependency on %s", item.name, i)
continue
logger.info("skip %s because it depends on %s", item.name, i)
pytest.skip("%s depends on %s" % (item.name, i))

Expand Down Expand Up @@ -170,3 +179,10 @@ def pytest_runtest_setup(item):
scope = marker.kwargs.get('scope', 'module')
manager = DependencyManager.getManager(item, scope=scope)
manager.checkDepend(depends, item)


def pytest_addhooks(pluginmanager):
"""This example assumes the hooks are grouped in the 'sample_hook' module."""
import pytest_dependency_hooks

pluginmanager.add_hookspecs(pytest_dependency_hooks)
9 changes: 9 additions & 0 deletions src/pytest_dependency_hooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import pytest

def pytest_dependency_override_skip(item: pytest.Item, dependency: str, scope: str) -> bool:
"""
Possibly override decision to skip a test item with a dependency.

If any implementation of this hook returns a true value `dependency`
being unknown or skipped will not cause `item` to be skipped.
"""