-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pytest-rich doesn't play well with pytest_runtest_makereport #74
Comments
I think that I found a workaround. Basically I'm inheriting from import inspect
import pytest
from attr import define
from pytest_rich import RichTerminalReporter
from rich.progress import Progress
@define(slots=False, hash=True)
class CustomRichTerminalReporter(RichTerminalReporter):
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_setup(self, item):
self.docstring = inspect.getdoc(item.obj).strip()
self.filename = item.config.args[0]
yield
def pytest_runtest_logfinish(self) -> None:
super().pytest_runtest_logfinish()
verbose = self.config.getoption("verbose")
if self.docstring and verbose > 0:
self.runtest_progress.stop()
docstring_progress = Progress(
"{task.description}",
)
docstring_task = docstring_progress.add_task("Print docstring")
docstring_progress.start()
docstring_progress.update(
docstring_task,
description=f"[bold green]{self.docstring} <- {self.filename}[/bold green]",
visible=True,
refresh=True,
)
docstring_progress.stop()
@pytest.hookimpl(trylast=True)
def pytest_configure(config):
rich_enabled = config.getoption("rich")
verbose = config.getoption("verbose")
rich_terminal_reporter = config.pluginmanager.getplugin("rich-terminal-reporter")
if rich_enabled and verbose:
config.pluginmanager.unregister(rich_terminal_reporter)
custom_terminal_reporter = CustomRichTerminalReporter(config)
config.pluginmanager.register(custom_terminal_reporter, "testrichreportdocstring") And now when I run the test with dev/random_code/tests via 🐍 v3.10.7 (tests)
❯ pytest test.py --rich -v
[100%] test.py ✔
⠋ Percent: 100%
This is a test with a documentation that should show up on the pytest report <- test.py |
Thanks for the detailed issue report @mazulo! It appears you're using the latest version published to PyPI. Development on the package went through a lull over the summer and has since picked back up, however a new version has not been released yet with all the recent changes. Do you mind testing with the latest version on pip install git+https://github.com/nicoddemus/pytest-rich.git (I suspect you'll still run in to the same issue.) |
Reference: Note: this is just so I/someone else doesn't have to go hunting for this when working on this issue in the future |
Hey @joshuadavidthomas thanks for the response! I was just testing it after installing the latest version on main. So for the first example where I use the random_code/tests/attempt2 via 🐍 v3.10.7 (tests) ❯ pytest test.py --verbose --rich
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/attempt2/conftest.py", line 26, in pytest_runtest_logfinish
INTERNALERROR> if self.terminal_reporter.verbosity == 0:
[100%] test.py ✔
⠋ Progress
Traceback (most recent call last):
File "/home/mazulo/.virtualenvs/tests/bin/pytest", line 8, in <module>
sys.exit(console_main())
File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/_pytest/config/__init__.py", line 190, in console_main
code = main()
File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/_pytest/config/__init__.py", line 167, in main
ret: Union[ExitCode, int] = config.hook.pytest_cmdline_main(
File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/pluggy/_hooks.py", line 265, in __call__
return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/pluggy/_manager.py", line 80, in _hookexec
return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/pluggy/_callers.py", line 60, in _multicall
return outcome.get_result()
File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/pluggy/_result.py", line 60, in get_result
raise ex[1].with_traceback(ex[2])
File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/pluggy/_callers.py", line 39, in _multicall
res = hook_impl.function(*args)
File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/_pytest/main.py", line 317, in pytest_cmdline_main
return wrap_session(config, _main)
File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/_pytest/main.py", line 305, in wrap_session
config.hook.pytest_sessionfinish(
File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/pluggy/_hooks.py", line 265, in __call__
return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/pluggy/_manager.py", line 80, in _hookexec
return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/pluggy/_callers.py", line 60, in _multicall
return outcome.get_result()
File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/pluggy/_result.py", line 60, in get_result
raise ex[1].with_traceback(ex[2])
File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/pluggy/_callers.py", line 39, in _multicall
res = hook_impl.function(*args)
File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/pytest_rich/terminal.py", line 221, in pytest_sessionfinish
self.print_summary(error_messages)
File "/home/mazulo/.virtualenvs/tests/lib/python3.10/site-packages/pytest_rich/terminal.py", line 272, in print_summary
f"({100 * no_of_items / self.total_items_completed:.1f}%)",
ZeroDivisionError: division by zero I'm still going to investigate further this error and see if I can find a solution. |
It looks like that the
pytest-rich
doesn't pay well with a custompytest_runtest_makereport
. I've the following example to reproduce the issue:If I ran the test with
pytest test.py --verbose --rich
this is what I get:However if I remove the
--rich
from there, it works just fine:Something more weird happens when I use the approach outlined here: https://stackoverflow.com/a/61002385/3716354
This is the result of running
pytest test.py -vvv --rich
(actually this same error happens when running without the-vvv
flag):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.The text was updated successfully, but these errors were encountered: