It looks like that the pytest-rich
doesn't pay well with a custom pytest_runtest_makereport
. I've the following example to reproduce the issue:
# conftest.py
import pytest
from _pytest.reports import TestReport
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
outcome = yield
if item.config.getvalue("verbose") > 0:
report = outcome.get_result()
test_fn = item.obj
docstring: str = getattr(test_fn, '__doc__')
test_name = report.nodeid.split("::")[0]
if docstring:
report.nodeid = f"{docstring.strip()} <- {test_name}"
else:
when = call.when
duration = call.stop - call.start
keywords = dict([(x, 1) for x in item.keywords])
sections = []
default_test_report = TestReport(
item.nodeid,
item.location,
keywords,
outcome,
None,
when,
sections,
duration,
)
return default_test_report
# test.py
def test_one_plus_one_equals_2():
"""
This is a test with a documentation that should show up on the pytest report
"""
assert 1 == 1
If I ran the test with pytest test.py --verbose --rich
this is what I get:
โญโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ pytest session starts โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎโ platform linux pytest 7.2.0 python 3.10.7 โโ root /home/mazulo/dev/random_code/tests โโฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏCollected 1 items
โ [ 0%] test.py
โ Progress INTERNALERROR> Traceback (most recent call last):
INTERNALERROR> File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/_pytest/main.py", line 270, in wrap_session
INTERNALERROR> session.exitstatus = doit(config, session) or 0
INTERNALERROR> File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/_pytest/main.py", line 324, in _main
INTERNALERROR> config.hook.pytest_runtestloop(session=session)
INTERNALERROR> File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/pluggy/_hooks.py", line 265, in __call__
INTERNALERROR> return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
INTERNALERROR> File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/pluggy/_manager.py", line 80, in _hookexec
INTERNALERROR> return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
INTERNALERROR> File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/pluggy/_callers.py", line 60, in _multicall
INTERNALERROR> return outcome.get_result()
INTERNALERROR> File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/pluggy/_result.py", line 60, in get_result
INTERNALERROR> raise ex[1].with_traceback(ex[2])
INTERNALERROR> File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/pluggy/_callers.py", line 39, in _multicall
INTERNALERROR> res = hook_impl.function(*args)
INTERNALERROR> File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/_pytest/main.py", line 349, in pytest_runtestloop
INTERNALERROR> item.config.hook.pytest_runtest_protocol(item=item, nextitem=nextitem)
INTERNALERROR> File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/pluggy/_hooks.py", line 265, in __call__
INTERNALERROR> return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
INTERNALERROR> File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/pluggy/_manager.py", line 80, in _hookexec
INTERNALERROR> return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
INTERNALERROR> File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/pluggy/_callers.py", line 60, in _multicall
INTERNALERROR> return outcome.get_result()
INTERNALERROR> File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/pluggy/_result.py", line 60, in get_result
INTERNALERROR> raise ex[1].with_traceback(ex[2])
INTERNALERROR> File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/pluggy/_callers.py", line 39, in _multicall
INTERNALERROR> res = hook_impl.function(*args)
INTERNALERROR> File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/_pytest/runner.py", line 112, in pytest_runtest_protocol
INTERNALERROR> runtestprotocol(item, nextitem=nextitem)
INTERNALERROR> File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/_pytest/runner.py", line 125, in runtestprotocol
INTERNALERROR> rep = call_and_report(item, "setup", log)
INTERNALERROR> File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/_pytest/runner.py", line 224, in call_and_report
INTERNALERROR> hook.pytest_runtest_logreport(report=report)
INTERNALERROR> File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/pluggy/_hooks.py", line 265, in __call__
INTERNALERROR> return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
INTERNALERROR> File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/pluggy/_manager.py", line 80, in _hookexec
INTERNALERROR> return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
INTERNALERROR> File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/pluggy/_callers.py", line 60, in _multicall
INTERNALERROR> return outcome.get_result()
INTERNALERROR> File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/pluggy/_result.py", line 60, in get_result
INTERNALERROR> raise ex[1].with_traceback(ex[2])
INTERNALERROR> File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/pluggy/_callers.py", line 39, in _multicall
INTERNALERROR> res = hook_impl.function(*args)
INTERNALERROR> File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/pytest_rich.py", line 225, in pytest_runtest_logreport
INTERNALERROR> self._update_task(report.nodeid)
INTERNALERROR> File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/pytest_rich.py", line 194, in _update_task
INTERNALERROR> task = self.runtest_tasks_per_file[fn]
โ [ 0%] test.py
โ Progress
However if I remove the --rich
from there, it works just fine:
dev/random_code/tests via ๐ v3.10.7 (tests)
โฏ pytest test.py --verbose
===================================================================================================================================================================================================== test session starts ======================================================================================================================================================================================================platform linux -- Python 3.10.7, pytest-7.2.0, pluggy-1.0.0 -- /home/mazulo/.virtualenvs/tests/bin/python
cachedir: .pytest_cache
rootdir: /home/mazulo/dev/random_code/tests
plugins: spec-3.2.0, rich-0.1.1
collected 1 item
test.py::test_one_plus_one_equals_2
This is a test with a documentation that should show up on the pytest report <- test.py PASSED [100%]
====================================================================================================================================================================================================== 1 passed in 0.00s =======================================================================================================================================================================================================
Something more weird happens when I use the approach outlined here: https://stackoverflow.com/a/61002385/3716354
# conftest.py
import inspect
import pytest
@pytest.hookimpl(trylast=True)
def pytest_configure(config):
terminal_reporter = config.pluginmanager.getplugin("terminalreporter")
config.pluginmanager.register(
TestReportDocstringPlugin(terminal_reporter), "testreportdocstring"
)
class TestReportDocstringPlugin:
def __init__(self, terminal_reporter):
self.terminal_reporter = terminal_reporter
self.docstring = None
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_setup(self, item):
self.docstring = inspect.getdoc(item.obj)
yield
@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_logfinish(self, nodeid: str, location):
if self.terminal_reporter.verbosity == 0:
yield
else:
if self.docstring:
test_name = nodeid.split("::")[0]
docstring = self.docstring.strip()
self.terminal_reporter.write(f"\n{docstring} <- {test_name}")
yield
This is the result of running pytest test.py -vvv --rich
(actually this same error happens when running without the -vvv
flag):
dev/random_code/tests via ๐ v3.10.7 (tests)
โฏ pytest test.py -vvv --rich
โญโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ pytest session starts โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎโ platform linux pytest 7.2.0 python 3.10.7 โโ root /home/mazulo/dev/random_code/tests โโฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏCollected 1 items
[100%] test.py โ
โ Progress INTERNALERROR> Traceback (most recent call last):
INTERNALERROR> File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/_pytest/main.py", line 270, in wrap_session
INTERNALERROR> session.exitstatus = doit(config, session) or 0
INTERNALERROR> File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/_pytest/main.py", line 324, in _main
INTERNALERROR> config.hook.pytest_runtestloop(session=session)
INTERNALERROR> File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/pluggy/_hooks.py", line 265, in __call__
INTERNALERROR> return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
INTERNALERROR> File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/pluggy/_manager.py", line 80, in _hookexec
INTERNALERROR> return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
INTERNALERROR> File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/pluggy/_callers.py", line 60, in _multicall
INTERNALERROR> return outcome.get_result()
INTERNALERROR> File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/pluggy/_result.py", line 60, in get_result
INTERNALERROR> raise ex[1].with_traceback(ex[2])
INTERNALERROR> File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/pluggy/_callers.py", line 39, in _multicall
INTERNALERROR> res = hook_impl.function(*args)
INTERNALERROR> File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/_pytest/main.py", line 349, in pytest_runtestloop
INTERNALERROR> item.config.hook.pytest_runtest_protocol(item=item, nextitem=nextitem)
INTERNALERROR> File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/pluggy/_hooks.py", line 265, in __call__
INTERNALERROR> return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
INTERNALERROR> File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/pluggy/_manager.py", line 80, in _hookexec
INTERNALERROR> return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
INTERNALERROR> File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/pluggy/_callers.py", line 60, in _multicall
INTERNALERROR> return outcome.get_result()
INTERNALERROR> File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/pluggy/_result.py", line 60, in get_result
INTERNALERROR> raise ex[1].with_traceback(ex[2])
INTERNALERROR> File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/pluggy/_callers.py", line 39, in _multicall
INTERNALERROR> res = hook_impl.function(*args)
INTERNALERROR> File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/_pytest/runner.py", line 113, in pytest_runtest_protocol
INTERNALERROR> ihook.pytest_runtest_logfinish(nodeid=item.nodeid, location=item.location)
INTERNALERROR> File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/pluggy/_hooks.py", line 265, in __call__
INTERNALERROR> return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
INTERNALERROR> File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/pluggy/_manager.py", line 80, in _hookexec
INTERNALERROR> return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
INTERNALERROR> File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/pluggy/_callers.py", line 60, in _multicall
INTERNALERROR> return outcome.get_result()
INTERNALERROR> File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/pluggy/_result.py", line 60, in get_result
INTERNALERROR> raise ex[1].with_traceback(ex[2])
INTERNALERROR> File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/pluggy/_callers.py", line 34, in _multicall
INTERNALERROR> next(gen) # first yield
INTERNALERROR> File "/home/mazulo/dev/random_code/tests/conftest.py", line 24, in pytest_runtest_logfinish
INTERNALERROR> if self.terminal_reporter.verbosity == 0:
[100%] test.py โ
โ Progress
For some reason it seems to get stuck at if self.terminal_reporter.verbosity == 0
what shouldn't happen since we're passing the -vvv
flag. So it seems that for some reason this flag is being ignored.
bug pytest parity