From 0f5fb7ed05c7911bbfe1583491f735c205453c34 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 29 Nov 2017 00:54:14 +0100 Subject: [PATCH 1/2] Fix ZeroDivisionError with 0 collected tests This can easily happen with pytest-testmon. --- _pytest/terminal.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/_pytest/terminal.py b/_pytest/terminal.py index 8538ee6aa..c5cbe14a1 100644 --- a/_pytest/terminal.py +++ b/_pytest/terminal.py @@ -313,8 +313,11 @@ class TerminalReporter: _PROGRESS_LENGTH = len(' [100%]') def _get_progress_information_message(self): - progress = self._progress_items_reported * 100 // self._session.testscollected - return ' [{:3d}%]'.format(progress) + collected = self._session.testscollected + if collected: + progress = self._progress_items_reported * 100 // collected + return ' [{:3d}%]'.format(progress) + return ' [100%]' def _write_progress_information_filling_space(self): if not self._show_progress_info: From 6bbd7410397fc3ea95b48cf38a3d8070ebf80fb7 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Tue, 28 Nov 2017 22:42:52 -0200 Subject: [PATCH 2/2] Add test for #2971 --- changelog/2971.bugfix | 1 + testing/test_terminal.py | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 changelog/2971.bugfix diff --git a/changelog/2971.bugfix b/changelog/2971.bugfix new file mode 100644 index 000000000..36684e8c8 --- /dev/null +++ b/changelog/2971.bugfix @@ -0,0 +1 @@ +Fix ``ZeroDivisionError`` when using the ``testmon`` plugin when no tests were actually collected. diff --git a/testing/test_terminal.py b/testing/test_terminal.py index 98a8ca121..97c2f71fb 100644 --- a/testing/test_terminal.py +++ b/testing/test_terminal.py @@ -988,6 +988,24 @@ class TestProgress: """, ) + def test_zero_tests_collected(self, testdir): + """Some plugins (testmon for example) might issue pytest_runtest_logreport without any tests being + actually collected (#2971).""" + testdir.makeconftest(""" + def pytest_collection_modifyitems(items, config): + from _pytest.runner import CollectReport + for node_id in ('nodeid1', 'nodeid2'): + rep = CollectReport(node_id, 'passed', None, None) + rep.when = 'passed' + rep.duration = 0.1 + config.hook.pytest_runtest_logreport(report=rep) + """) + output = testdir.runpytest() + assert 'ZeroDivisionError' not in output.stdout.str() + output.stdout.fnmatch_lines([ + '=* 2 passed in *=', + ]) + def test_normal(self, many_tests_file, testdir): output = testdir.runpytest() output.stdout.re_match_lines([