nvim-neotest / neotest-python

MIT License
115 stars 34 forks source link

Distinguish between instances of parametrized tests in diagnostic message #30

Closed OddBloke closed 1 year ago

OddBloke commented 1 year ago

I make heavy use of parameterised tests: a diagnostic message telling me the line causing the failure for one of many test instances isn't especially useful. It would be good to prefix(/otherwise attach?) information which identifies the specific instance(s) which are failing.

OddBloke commented 1 year ago
diff --git a/neotest_python/pytest.py b/neotest_python/pytest.py
index 7b33ea6..d80f7fa 100644
--- a/neotest_python/pytest.py
+++ b/neotest_python/pytest.py
@@ -103,11 +103,15 @@ class NeotestResultCollector:
         errors: List[NeotestError] = []
         short = self._get_short_output(self.pytest_config, report)

+        msg_prefix = ""
+        if getattr(item, "callspec", None) is not None:
+            # Parametrized test
+            msg_prefix = f"[{item.callspec.id}] "
         if report.outcome == "failed":
             exc_repr = report.longrepr
             # Test fails due to condition outside of test e.g. xfail
             if isinstance(exc_repr, str):
-                errors.append({"message": exc_repr, "line": None})
+                errors.append({"message": msg_prefix + exc_repr, "line": None})
             # Test failed internally
             elif isinstance(exc_repr, ExceptionRepr):
                 error_message = exc_repr.reprcrash.message  # type: ignore
@@ -115,7 +119,7 @@ class NeotestResultCollector:
                 for traceback_entry in reversed(call.excinfo.traceback):
                     if str(traceback_entry.path) == abs_path:
                         error_line = traceback_entry.lineno
-                errors.append({"message": error_message, "line": error_line})
+                errors.append({"message": msg_prefix + error_message, "line": error_line})
             elif isinstance(exc_repr, ReprFailDoctest):
                 error_line = error_message = None
                 for reprlocation_line, _ in exc_repr.reprlocation_lines:

is doing the trick for me locally (on top of #29, so I'll hold off on proposing a PR).