Skip to content

Commit

Permalink
handle duplicate test ids via collection and xdist each reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
RonnyPfannschmidt committed Dec 7, 2015
1 parent 04e9ae7 commit 7b7737b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
20 changes: 13 additions & 7 deletions _pytest/junitxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,12 +262,18 @@ def __init__(self, logfile, prefix):
self.node_reporters = {} # nodeid -> _NodeReporter
self.node_reporters_ordered = []

def node_reporter(self, nodeid):
if nodeid in self.node_reporters:
def node_reporter(self, report):
nodeid = getattr(report, 'nodeid', report)
# local hack to handle xdist report order
slavenode = getattr(report, 'node', None)

key = nodeid, slavenode

if key in self.node_reporters:
#TODO: breasks for --dist=each
return self.node_reporters[nodeid]
return self.node_reporters[key]
reporter = _NodeReporter(nodeid, self)
self.node_reporters[nodeid] = reporter
self.node_reporters[key] = reporter
self.node_reporters_ordered.append(reporter)
return reporter

Expand All @@ -276,7 +282,7 @@ def add_stats(self, key):
self.stats[key] += 1

def _opentestcase(self, report):
reporter = self.node_reporter(report.nodeid)
reporter = self.node_reporter(report)
reporter.record_testreport(report)
return reporter

Expand Down Expand Up @@ -318,13 +324,13 @@ def pytest_runtest_logreport(self, report):
reporter.append_skipped(report)
self.update_testcase_duration(report)
if report.when == "teardown":
self.node_reporter(report.nodeid).finalize()
self.node_reporter(report).finalize()

def update_testcase_duration(self, report):
"""accumulates total duration for nodeid from given report and updates
the Junit.testcase with the new total if already created.
"""
reporter = self.node_reporter(report.nodeid)
reporter = self.node_reporter(report)
reporter.duration += getattr(report, 'duration', 0.0)

def pytest_collectreport(self, report):
Expand Down
16 changes: 14 additions & 2 deletions testing/test_junitxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -661,12 +661,24 @@ def test_x(i):
assert failed == ['test_x[22]']


@pytest.mark.xfail(reason='duplicate test ids kill us')
def test_runs_twice(testdir):
f = testdir.makepyfile('''
def test_pass():
pass
''')

result = testdir.runpytest(f, f, '--junitxml', testdir.tmpdir.join("test.xml"))
assert 'INTERNALERROR' not in result.stdout
assert 'INTERNALERROR' not in str(result.stdout)


def test_runs_twice_xdist(testdir):
pytest.importorskip('xdist')
f = testdir.makepyfile('''
def test_pass():
pass
''')

result = testdir.runpytest(f,
'--dist', 'each', '--tx', '2*popen',
'--junitxml', testdir.tmpdir.join("test.xml"))
assert 'INTERNALERROR' not in str(result.stdout)

0 comments on commit 7b7737b

Please sign in to comment.