Skip to content

Remove parametrization when storing dependency results #43

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

Open
wants to merge 6 commits 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ __pycache__/
/doc/latex/
/doc/linkcheck/
/pytest_dependency.egg-info/
/src/pytest_dependency.egg-info/
/python2_6.patch
47 changes: 37 additions & 10 deletions src/pytest_dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class DependencyItemStatus(object):
Phases = ('setup', 'call', 'teardown')

def __init__(self):
self.results = { w:None for w in self.Phases }
self.results = {w: None for w in self.Phases}

def __str__(self):
l = ["%s: %s" % (w, self.results[w]) for w in self.Phases]
Expand All @@ -30,6 +30,9 @@ def addResult(self, rep):
def isSuccess(self):
return list(self.results.values()) == ['passed', 'passed', 'passed']

def isDone(self):
return None not in self.results.values()


class DependencyManager(object):
"""Dependency manager, stores the results of tests.
Expand Down Expand Up @@ -73,10 +76,28 @@ def addResult(self, item, name, rep):
else:
raise RuntimeError("Internal error: invalid scope '%s'"
% self.scope)
status = self.results.setdefault(name, DependencyItemStatus())
# store an extra result for parameterless name
# this enables dependencies based on an overall test status
original = item.originalname if item.originalname is not None else item.name
if not name.endswith(original):
# remove the parametrization part at the end
index = name.rindex(original) + len(original)
parameterless_name = name[:index]
if parameterless_name not in self.results:
self.results[parameterless_name] = DependencyItemStatus()
status = self.results[parameterless_name]
# only add the result if the status is incomplete or it's (still) a success
# this prevents overwriting a failed status of one parametrized test,
# with a success status of the following tests
if not status.isDone() or status.isSuccess():
status.addResult(rep)

if name not in self.results:
self.results[name] = DependencyItemStatus()
# add the result
logger.debug("register %s %s %s in %s scope",
rep.when, name, rep.outcome, self.scope)
status.addResult(rep)
self.results[name].addResult(rep)

def checkDepend(self, depends, item):
logger.debug("check dependencies of %s in %s scope ...",
Expand Down Expand Up @@ -126,19 +147,25 @@ def depends(request, other, scope='module'):


def pytest_addoption(parser):
parser.addini("automark_dependency",
"Add the dependency marker to all tests automatically",
type="bool", default=False)
parser.addoption("--ignore-unknown-dependency",
action="store_true", default=False,
help="ignore dependencies whose outcome is not known")
parser.addini(
"automark_dependency",
"Add the dependency marker to all tests automatically",
type="bool",
default=False,
)
parser.addoption(
"--ignore-unknown-dependency",
action="store_true",
default=False,
help="ignore dependencies whose outcome is not known",
)


def pytest_configure(config):
global _automark, _ignore_unknown
_automark = config.getini("automark_dependency")
_ignore_unknown = config.getoption("--ignore-unknown-dependency")
config.addinivalue_line("markers",
config.addinivalue_line("markers",
"dependency(name=None, depends=[]): "
"mark a test to be used as a dependency for "
"other tests or to depend on other tests.")
Expand Down
52 changes: 52 additions & 0 deletions tests/test_03_param.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,58 @@
import pytest


def test_removed_params(ctestdir):
"""
Test for a dependency on a parametrized test, but with parametrization removed.
"""
ctestdir.makepyfile("""
import pytest

@pytest.mark.parametrize("x", [ 0, 1 ])
@pytest.mark.dependency()
def test_a(x):
# passes, then fails
assert x == 0

@pytest.mark.parametrize("x", [ 0, 1 ])
@pytest.mark.dependency()
def test_b(x):
# fails, then passes
assert x == 1

@pytest.mark.parametrize("x", [ 0, 1 ])
@pytest.mark.dependency()
def test_c(x):
# always passes
pass

@pytest.mark.dependency(depends=["test_a"])
def test_d():
pass

@pytest.mark.dependency(depends=["test_b"])
def test_e():
pass

@pytest.mark.dependency(depends=["test_c"])
def test_f():
pass
""")
result = ctestdir.runpytest("--verbose")
result.assert_outcomes(passed=5, skipped=2, failed=2)
result.stdout.re_match_lines(r"""
.*::test_a\[0\] PASSED
.*::test_a\[1\] FAILED
.*::test_b\[0\] FAILED
.*::test_b\[1\] PASSED
.*::test_c\[0\] PASSED
.*::test_c\[1\] PASSED
.*::test_d SKIPPED(?:\s+\(.*\))?
.*::test_e SKIPPED(?:\s+\(.*\))?
.*::test_f PASSED
""")


def test_simple_params(ctestdir):
"""Simple test for a dependency on a parametrized test.

Expand Down