[Python] Filter out unneeded lines in pytest stack traces? [closed]

Discussão em 'Python' iniciado por Stack, Janeiro 23, 2025.

  1. Stack

    Stack Membro Participativo

    Pytest was installed by using pip install pytest.

    Pytest is being run at a command prompt (W10) not from inside an IDE. Typically:

    > pytest --random-order -k test_my_test_module


    (NB --random-order comes from package pytest-random-order. I use this switch every time I run pytest, without exception, to such an extent that I actually run pytest using a python script which does that)

    I found this page and tried options like --tb=short and -q. They don't seem to have the effect on the stack trace that I'm looking for. Here's an example:

    Stack (most recent call last):
    File "C:\Users\Mike\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
    File "C:\Users\Mike\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
    File "D:\apps\Python\virtual_envs\dev_organiser\Scripts\pytest.exe\__main__.py", line 7, in <module>
    sys.exit(console_main())
    File "D:\apps\Python\virtual_envs\dev_organiser\lib\site-packages\_pytest\config\__init__.py", line 206, in console_main
    code = main()
    File "D:\apps\Python\virtual_envs\dev_organiser\lib\site-packages\_pytest\config\__init__.py", line 178, in main
    ret: Union[ExitCode, int] = config.hook.pytest_cmdline_main(
    File "D:\apps\Python\virtual_envs\dev_organiser\lib\site-packages\pluggy\_hooks.py", line 513, in __call__
    return self._hookexec(self.name, self._hookimpls.copy(), kwargs, firstresult)
    File "D:\apps\Python\virtual_envs\dev_organiser\lib\site-packages\pluggy\_manager.py", line 120, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
    File "D:\apps\Python\virtual_envs\dev_organiser\lib\site-packages\pluggy\_callers.py", line 103, in _multicall
    res = hook_impl.function(*args)
    File "D:\apps\Python\virtual_envs\dev_organiser\lib\site-packages\_pytest\main.py", line 332, in pytest_cmdline_main
    return wrap_session(config, _main)
    File "D:\apps\Python\virtual_envs\dev_organiser\lib\site-packages\_pytest\main.py", line 285, in wrap_session
    session.exitstatus = doit(config, session) or 0
    File "D:\apps\Python\virtual_envs\dev_organiser\lib\site-packages\_pytest\main.py", line 339, in _main
    config.hook.pytest_runtestloop(session=session)
    File "D:\apps\Python\virtual_envs\dev_organiser\lib\site-packages\pluggy\_hooks.py", line 513, in __call__
    return self._hookexec(self.name, self._hookimpls.copy(), kwargs, firstresult)
    File "D:\apps\Python\virtual_envs\dev_organiser\lib\site-packages\pluggy\_manager.py", line 120, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
    File "D:\apps\Python\virtual_envs\dev_organiser\lib\site-packages\pluggy\_callers.py", line 103, in _multicall
    res = hook_impl.function(*args)
    File "D:\apps\Python\virtual_envs\dev_organiser\lib\site-packages\_pytest\main.py", line 364, in pytest_runtestloop
    item.config.hook.pytest_runtest_protocol(item=item, nextitem=nextitem)
    File "D:\apps\Python\virtual_envs\dev_organiser\lib\site-packages\pluggy\_hooks.py", line 513, in __call__
    return self._hookexec(self.name, self._hookimpls.copy(), kwargs, firstresult)
    File "D:\apps\Python\virtual_envs\dev_organiser\lib\site-packages\pluggy\_manager.py", line 120, in _hookexec
    ...


    (and on and on and on...)

    It may be that this sort of phenomenon is specific to my setup for some reason. But I really don't need all this nonsense about "pluggy", etc., each time I'm trying to track what may have occurred.

    I'd be helpful to know whether people typtically get something different to this during normal runs of pytest at a command prompt. Is this atypical output? Currently I have no way of knowing.

    In Python logging module there is a method in the most basic Formatter class (superclass of all others) which you can implement (basic version does nothing): formatStack(stack_info). This gives you one parameter, a multiline string, which you can edit to cut out unnecessary lines. If Pytest doesn't have an "official" way of configuring its logger formatters to cut out uninteresting stack lines I'll have to try to see whether this is one way to try and do things.

    Continue reading...

Compartilhe esta Página