Skip to content

Commit

Permalink
Merge pull request #53 from davehunt/xdist-support
Browse files Browse the repository at this point in the history
Add support for pytest-xdist
  • Loading branch information
sallner authored Aug 28, 2017
2 parents d252d42 + f61f658 commit 5d528e5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
4 changes: 1 addition & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,7 @@ Here's an example of the output provided by the plugin when run with
======================= 1 failed, 2 rerun in 0.02 seconds ====================

Note that output will show all re-runs. Tests that fail on all the re-runs will
be marked as failed. Due to a
`current limitation in pytest-xdist <https://github.com/pytest-dev/pytest/issues/1193>`_,
when running tests in parallel only the final result will be included in the output.
be marked as failed.

Compatibility
-------------
Expand Down
30 changes: 21 additions & 9 deletions pytest_rerunfailures.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
import pkg_resources

import pytest

from _pytest.runner import runtestprotocol
from _pytest.resultlog import ResultLog


def works_with_current_xdist():
"""Returns compatibility with installed pytest-xdist version.
When running tests in parallel using pytest-xdist < 1.20.0, the first
report that is logged will finish and terminate the current node rather
rerunning the test. Thus we must skip logging of intermediate results under
these circumstances, otherwise no test is rerun.
"""
try:
d = pkg_resources.get_distribution('pytest-xdist')
return d.parsed_version >= pkg_resources.parse_version('1.20')
except pkg_resources.DistributionNotFound:
return None


# command line options
def pytest_addoption(parser):
group = parser.getgroup(
Expand Down Expand Up @@ -69,6 +87,8 @@ def pytest_runtest_protocol(item, nextitem):
# first item if necessary
check_options(item.session.config)

parallel = hasattr(item.config, 'slaveinput')

for i in range(reruns + 1): # ensure at least one run of each item
item.ihook.pytest_runtest_logstart(nodeid=item.nodeid,
location=item.location)
Expand All @@ -84,14 +104,7 @@ def pytest_runtest_protocol(item, nextitem):
# failure detected and reruns not exhausted, since i < reruns
report.outcome = 'rerun'

# When running tests in parallel using pytest-xdist the first
# report that is logged will finish and terminate the current
# node rather rerunning the test. Thus we must skip logging of
# intermediate results when running in parallel, otherwise no
# test is rerun.
# See: https://github.com/pytest-dev/pytest/issues/1193
parallel_testing = hasattr(item.config, 'slaveinput')
if not parallel_testing:
if not parallel or works_with_current_xdist():
# will rerun test, log intermediate result
item.ihook.pytest_runtest_logreport(report=report)

Expand Down Expand Up @@ -162,4 +175,3 @@ def pytest_runtest_logreport(self, report):
longrepr = str(report.longrepr)

self.log_outcome(report, code, longrepr)

0 comments on commit 5d528e5

Please sign in to comment.