Skip to content

Commit

Permalink
Skip deselection of items without collector (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
numirias committed Sep 24, 2021
1 parent bd58bd5 commit af77e4b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
5 changes: 4 additions & 1 deletion pytest_jsonreport/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,10 @@ def pytest_deselected(self, items):
if self._must_omit('collectors'):
return
for item in items:
item._json_collectitem['deselected'] = True
try:
item._json_collectitem['deselected'] = True
except AttributeError:
continue

@pytest.hookimpl(hookwrapper=True)
def pytest_collection_modifyitems(self, items):

This comment has been minimized.

Copy link
@guillp

guillp Dec 17, 2021

FYI the exact same issue happens in this method as well, the del item._json_collectitem must be protected by a try: except AttributeError: pass

Expand Down
15 changes: 15 additions & 0 deletions tests/test_jsonreport.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,3 +592,18 @@ def test_bug_41(misc_testdir):
"""#41: Create report file path if it doesn't exist."""
misc_testdir.runpytest('--json-report', '--json-report-file=x/report.json')
assert (misc_testdir.tmpdir / 'x/report.json').exists()


def test_bug_69(testdir):
"""#69: Handle deselection of test items that have not been collected."""
fn = testdir.makepyfile('''
def test_pass():
assert True
def test_fail():
assert False
''').strpath
assert testdir.runpytest('--json-report', fn).ret == 1
# In this second run, `--last-failed` causes `test_pass` to not be
# *collected* but still explicitly *deselected*, so we assert there is no
# internal error caused by trying to access the collector obj.
assert testdir.runpytest('--json-report', '--last-failed', fn).ret == 1

0 comments on commit af77e4b

Please sign in to comment.