Skip to content

Commit 426ed0b

Browse files
authored
refactor: enhance error reporting in pytest output for better debugging (#3943)
1 parent e0e6f05 commit 426ed0b

7 files changed

Lines changed: 106 additions & 11 deletions

File tree

doc/changelog.d/3943.added.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
refactor: enhance error reporting in pytest output for better debugging

tests/conftest.py

Lines changed: 88 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -315,24 +315,106 @@ class MyReporter(TerminalReporter):
315315
def short_test_summary(self):
316316
# your own impl goes here, for example:
317317
self.write_sep("=", "PyMAPDL Pytest short summary")
318+
markup = self._tw.markup
319+
320+
if self.hasmarkup:
321+
color = True
322+
else:
323+
color = False
324+
325+
ERROR_COLOR = {"Red": color, "bold": True}
326+
FAILED_COLOR = {"red": color, "bold": True}
327+
PASSED_COLOR = {"green": color}
328+
SKIPPED_COLOR = {"green": color, "bold": True}
329+
XPASSED_COLOR = {"Yellow": color, "bold": True}
330+
XFAILED_COLOR = {"yellow": color}
331+
332+
# self._tw.markup("asdf", Red=True)
333+
334+
def get_normal_message(rep, header, message):
335+
location = rep.location
336+
if message:
337+
message = f" - {message}"
338+
339+
if location[0] == location[2]:
340+
return f"{header} {rep.head_line}{message}"
341+
else:
342+
path = f"{location[0]}:{location[1]}"
343+
return f"{header} {rep.head_line} - {path}{message}"
344+
345+
def get_failure_message(rep, header, message):
346+
location = rep.location
347+
path = f"{location[0]}:{location[1]}"
348+
cause = message.splitlines()
349+
cause = " ".join(
350+
[
351+
each[2:].strip() if each.startswith("E ") else each.strip()
352+
for each in cause
353+
]
354+
)
355+
356+
return f"{header} {rep.head_line} - {path}: {cause}"
357+
358+
def get_skip_message(rep):
359+
message = rep.longrepr[2]
360+
header = markup("[SKIPPED]", **SKIPPED_COLOR)
361+
return get_normal_message(rep, header, message)
362+
363+
def get_passed_message(rep):
364+
message = rep.longreprtext
365+
header = markup("[PASSED]", **PASSED_COLOR)
366+
return get_normal_message(rep, header, message)
367+
368+
def get_xfailed_message(rep):
369+
message = " ".join(rep.longrepr.reprcrash.message.split(":")[1:]).strip()
370+
header = markup("[XFAILED]", **XFAILED_COLOR)
371+
return get_normal_message(rep, header, message)
372+
373+
def get_xpassed_message(rep):
374+
message = rep.longreprtext
375+
header = markup("[XPASSED]", **XPASSED_COLOR)
376+
return get_normal_message(rep, header, message)
377+
378+
def get_error_message(rep):
379+
message = rep.longrepr.reprcrash.message
380+
header = markup("[ERROR]", **ERROR_COLOR)
381+
return get_failure_message(rep, header, message)
382+
383+
def get_failed_message(rep):
384+
message = rep.longrepr.reprcrash.message
385+
header = markup("[FAILED]", **FAILED_COLOR)
386+
return get_failure_message(rep, header, message)
318387

319388
failed = self.stats.get("failed", [])
320389
for rep in failed:
321-
self.write_line(
322-
f"[FAILED] {rep.head_line} - {rep.longreprtext.splitlines()[-3]}"
323-
)
390+
self.write_line(get_failed_message(rep))
391+
392+
skipped = self.stats.get("skipped", [])
393+
for rep in skipped:
394+
self.write_line(get_skip_message(rep))
324395

325396
errored = self.stats.get("error", [])
326397
for rep in errored:
327-
self.write_line(
328-
f"[ERROR] {rep.head_line} - {rep.longreprtext.splitlines()[-3]}"
329-
)
398+
self.write_line(get_error_message(rep))
399+
400+
passed = self.stats.get("passed", [])
401+
for rep in passed:
402+
self.write_line(get_passed_message(rep))
403+
404+
xpassed = self.stats.get("xpassed", [])
405+
for rep in xpassed:
406+
self.write_line(get_xpassed_message(rep))
407+
408+
xfailed = self.stats.get("xfailed", [])
409+
for rep in xfailed:
410+
self.write_line(get_xfailed_message(rep))
330411

331412

332413
@pytest.hookimpl(trylast=True)
333414
def pytest_configure(config):
334415
vanilla_reporter = config.pluginmanager.getplugin("terminalreporter")
335416
my_reporter = MyReporter(config)
417+
my_reporter._tw.fullwidth = 160
336418
config.pluginmanager.unregister(vanilla_reporter)
337419
config.pluginmanager.register(my_reporter, "terminalreporter")
338420

tests/test_krylov.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@
3131

3232
if not has_dependency("ansys-math-core"):
3333
# Needs ansys-math-core
34-
pytest.skip(allow_module_level=True)
34+
pytest.skip(
35+
allow_module_level=True,
36+
reason="Skipping because 'ansys-math-core' is not installed",
37+
)
3538

3639
PATH = os.path.dirname(os.path.abspath(__file__))
3740

tests/test_launcher_remote.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@
2626
from conftest import has_dependency
2727

2828
if not has_dependency("ansys-platform-instancemanagement"):
29-
pytest.skip(allow_module_level=True)
29+
pytest.skip(
30+
allow_module_level=True,
31+
reason="Skipping because 'ansys-platform-instancemanagement' is not installed",
32+
)
3033

3134
from unittest.mock import create_autospec
3235

tests/test_plotting.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@
3030
from conftest import has_dependency, requires
3131

3232
if not has_dependency("pyvista"):
33-
pytest.skip(allow_module_level=True)
33+
pytest.skip(
34+
allow_module_level=True, reason="Skipping because 'pyvista' is not installed"
35+
)
3436

3537
from ansys.mapdl.core.errors import ComponentDoesNotExits, MapdlRuntimeError
3638
from ansys.mapdl.core.plotting import GraphicsBackend

tests/test_pool.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@
5151

5252
# skipping if ON_STUDENT and ON_LOCAL because we cannot spawn that many instances.
5353
if ON_STUDENT:
54-
pytest.skip(allow_module_level=True)
54+
pytest.skip(
55+
allow_module_level=True, reason="Skipping Pool tests on student version."
56+
)
5557

5658

5759
skip_if_ignore_pool = pytest.mark.skipif(

tests/test_theme.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
from conftest import has_dependency
2626

2727
if not has_dependency("pyvista"):
28-
pytest.skip(allow_module_level=True)
28+
pytest.skip(
29+
allow_module_level=True, reason="Skipping because 'pyvista' is not installed"
30+
)
2931

3032
import matplotlib
3133
import numpy as np

0 commit comments

Comments
 (0)