Skip to content

Use test body as traceback when empty #1493

Closed
@max4t

Description

@max4t

Hi,
I've added a 'raises' marker for my tests and to handle these, i've created a runtest wrapper like this:

@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_call(item):
    #__tracebackhide__ = True
    if item.get_marker("raises"):
        with pytest.raises(item.get_marker("raises").kwargs['error']):
            outcome = (yield)
            outcome.get_result()
        outcome.force_result(None)
    else:
        yield

It works fine except for the traceback because if the test did not raise any exception, it's not in the trace anymore and the trace of the wrapper will be printed instead.
If i use the 'tracebackhide' variable to ignore the trace of the wrapper, the traceback is empty which raise an error like this:

.......
  File "./venv/lib/python3.4/site-packages/_pytest/_code/code.py", line 418, in getrepr
    return fmt.repr_excinfo(self)
  File "./venv/lib/python3.4/site-packages/_pytest/_code/code.py", line 598, in repr_excinfo
    reprtraceback = self.repr_traceback(excinfo)
  File "./venv/lib/python3.4/site-packages/_pytest/_code/code.py", line 585, in repr_traceback
    last = traceback[-1]
  File "./venv/lib/python3.4/site-packages/_pytest/_code/code.py", line 289, in __getitem__
    val = super(Traceback, self).__getitem__(key)
IndexError: list index out of range

Wouldn't it be useful to print the test body as traceback if it's empty? Instead of printing the pytest_runtest_call body (without tracebackhide) or raising an error (with tracebackhide).

It would be useful to mark cases of a parametrized test.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions