From b27785ef06cc0a21ff94e58e98f9e480189a72c3 Mon Sep 17 00:00:00 2001 From: Paul Morelle Date: Mon, 18 Feb 2019 16:29:25 +0100 Subject: [PATCH] dump_json: fix when longrepr has no toterminal Some plugins generate a longrepr which is a string, and does not have a 'toterminal' method. I had the case with pytest-pycodestyle 1.4.0 and pycodestyle 2.5.0. --- pytest_litf.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/pytest_litf.py b/pytest_litf.py index 47e4700..6c14770 100644 --- a/pytest_litf.py +++ b/pytest_litf.py @@ -141,11 +141,14 @@ def dump_json(self, reports): errors = {} for report in reports: if report.outcome == "failed" and report.longrepr: - # Compute human repre - tw = py.io.TerminalWriter(stringio=True) - tw.hasmarkup = False - report.longrepr.toterminal(tw) - exc = tw.stringio.getvalue() + if hasattr(report.longrepr, 'toterminal'): + # Compute human repre + tw = py.io.TerminalWriter(stringio=True) + tw.hasmarkup = False + report.longrepr.toterminal(tw) + exc = tw.stringio.getvalue() + else: + exc = str(report.longrepr) humanrepr = exc.strip() errors[report.when] = {"humanrepr": humanrepr}