fix(pytest): handle xfail exc repr

See #5
This commit is contained in:
Rónán Carrigan
2022-06-09 21:15:15 +01:00
parent e4a97894b5
commit 16ed9f6079

View File

@@ -5,7 +5,6 @@ from typing import TYPE_CHECKING, Dict, List, Optional, cast
from .base import NeotestAdapter, NeotestError, NeotestResult, NeotestResultStatus from .base import NeotestAdapter, NeotestError, NeotestResult, NeotestResultStatus
if TYPE_CHECKING: if TYPE_CHECKING:
from _pytest._code.code import ExceptionChainRepr
from _pytest.config import Config from _pytest.config import Config
from _pytest.reports import TestReport from _pytest.reports import TestReport
@@ -36,6 +35,7 @@ class PytestNeotestAdapter(NeotestAdapter):
def run(self, args: List[str]) -> Dict[str, NeotestResult]: def run(self, args: List[str]) -> Dict[str, NeotestResult]:
results: Dict[str, NeotestResult] = {} results: Dict[str, NeotestResult] = {}
pytest_config: "Config" pytest_config: "Config"
from _pytest._code.code import ExceptionChainRepr
class NeotestResultCollector: class NeotestResultCollector:
@staticmethod @staticmethod
@@ -57,18 +57,25 @@ class PytestNeotestAdapter(NeotestAdapter):
errors: List[NeotestError] = [] errors: List[NeotestError] = []
short = self.get_short_output(pytest_config, report) short = self.get_short_output(pytest_config, report)
if report.outcome == "failed": if report.outcome == "failed":
exc_repr = cast("ExceptionChainRepr", report.longrepr) exc_repr = report.longrepr
exc_repr.toterminal # Test fails due to condition outside of test e.g. xfail
reprtraceback = exc_repr.reprtraceback if isinstance(exc_repr, str):
error_message = exc_repr.reprcrash.message # type: ignore errors.append({"message": exc_repr, "line": None})
error_line = None # Test failed internally
for repr in reversed(reprtraceback.reprentries): elif isinstance(exc_repr, ExceptionChainRepr):
if ( reprtraceback = exc_repr.reprtraceback
hasattr(repr, "reprfileloc") error_message = exc_repr.reprcrash.message # type: ignore
and repr.reprfileloc.path == file_path error_line = None
): for repr in reversed(reprtraceback.reprentries):
error_line = repr.reprfileloc.lineno - 1 if (
errors.append({"message": error_message, "line": error_line}) hasattr(repr, "reprfileloc")
and repr.reprfileloc.path == file_path
):
error_line = repr.reprfileloc.lineno - 1
errors.append({"message": error_message, "line": error_line})
else:
# TODO: Figure out how these are returned and how to represent
raise Exception("Unhandled error type, please report to neotest-python repo")
pos_id = "::".join([abs_path, *namespaces, valid_test_name]) pos_id = "::".join([abs_path, *namespaces, valid_test_name])
results[pos_id] = self.update_result( results[pos_id] = self.update_result(
results.get(pos_id), results.get(pos_id),