numirias / pytest-json-report

🗒️ A pytest plugin to report test results as JSON
MIT License
147 stars 39 forks source link

Crashes when used with `--tb=native` option #62

Closed rciorba closed 3 years ago

rciorba commented 3 years ago

Hello and thanks for this supper useful plugin!

The plugin crashes if used together with --tb=native

Essentially this is because pytest's ReprEntryNative and ReprEntry classes don't have the same interface.

Here's an sample traceback:

INTERNALERROR>   File "/home/rciorba/repos/pytest-json-report/.tox/py37/lib/python3.7/site-packages/pytest_jsonreport/plugin.py", line 180, in pytest_runtest_logreport
INTERNALERROR>     self._config.hook.pytest_json_runtest_stage(report=report)
INTERNALERROR>   File "/home/rciorba/repos/pytest-json-report/.tox/py37/lib/python3.7/site-packages/pluggy/hooks.py", line 286, in __call__
INTERNALERROR>     return self._hookexec(self, self.get_hookimpls(), kwargs)
INTERNALERROR>   File "/home/rciorba/repos/pytest-json-report/.tox/py37/lib/python3.7/site-packages/pluggy/manager.py", line 93, in _hookexec
INTERNALERROR>     return self._inner_hookexec(hook, methods, kwargs)
INTERNALERROR>   File "/home/rciorba/repos/pytest-json-report/.tox/py37/lib/python3.7/site-packages/pluggy/manager.py", line 337, in traced_hookexec
INTERNALERROR>     return outcome.get_result()
INTERNALERROR>   File "/home/rciorba/repos/pytest-json-report/.tox/py37/lib/python3.7/site-packages/pluggy/callers.py", line 80, in get_result
INTERNALERROR>     raise ex[1].with_traceback(ex[2])
INTERNALERROR>   File "/home/rciorba/repos/pytest-json-report/.tox/py37/lib/python3.7/site-packages/pluggy/callers.py", line 52, in from_call
INTERNALERROR>     result = func()
INTERNALERROR>   File "/home/rciorba/repos/pytest-json-report/.tox/py37/lib/python3.7/site-packages/pluggy/manager.py", line 335, in <lambda>
INTERNALERROR>     outcome = _Result.from_call(lambda: oldcall(hook, hook_impls, kwargs))
INTERNALERROR>   File "/home/rciorba/repos/pytest-json-report/.tox/py37/lib/python3.7/site-packages/pluggy/manager.py", line 87, in <lambda>
INTERNALERROR>     firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
INTERNALERROR>   File "/home/rciorba/repos/pytest-json-report/.tox/py37/lib/python3.7/site-packages/pluggy/callers.py", line 208, in _multicall
INTERNALERROR>     return outcome.get_result()
INTERNALERROR>   File "/home/rciorba/repos/pytest-json-report/.tox/py37/lib/python3.7/site-packages/pluggy/callers.py", line 80, in get_result
INTERNALERROR>     raise ex[1].with_traceback(ex[2])
INTERNALERROR>   File "/home/rciorba/repos/pytest-json-report/.tox/py37/lib/python3.7/site-packages/pluggy/callers.py", line 187, in _multicall
INTERNALERROR>     res = hook_impl.function(*args)
INTERNALERROR>   File "/home/rciorba/repos/pytest-json-report/.tox/py37/lib/python3.7/site-packages/pytest_jsonreport/plugin.py", line 197, in pytest_json_runtest_stage
INTERNALERROR>     traceback,
INTERNALERROR>   File "/home/rciorba/repos/pytest-json-report/.tox/py37/lib/python3.7/site-packages/pytest_jsonreport/serialize.py", line 58, in make_teststage
INTERNALERROR>     stage['traceback'] = make_traceback(traceback)
INTERNALERROR>   File "/home/rciorba/repos/pytest-json-report/.tox/py37/lib/python3.7/site-packages/pytest_jsonreport/serialize.py", line 91, in make_traceback
INTERNALERROR>     } for entry in traceback.reprentries]
INTERNALERROR>   File "/home/rciorba/repos/pytest-json-report/.tox/py37/lib/python3.7/site-packages/pytest_jsonreport/serialize.py", line 91, in <listcomp>
INTERNALERROR>     } for entry in traceback.reprentries]
INTERNALERROR> AttributeError: 'ReprEntryNative' object has no attribute 'reprfileloc'
numirias commented 3 years ago

Thanks for your contribution!

Your analysis of the issue is spot-on. However, for the fix I decided to simply drop the traceback entry from the report if we failed to extract the necessary information. Using the string representation from e.g. ReprEntryNative.lines would just duplicate what we provide via longrepr anyway (that is, a textual error representation).

Also, I generalized the fix to catch all different possible --tb values.