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,8 +57,12 @@ 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
if isinstance(exc_repr, str):
errors.append({"message": exc_repr, "line": None})
# Test failed internally
elif isinstance(exc_repr, ExceptionChainRepr):
reprtraceback = exc_repr.reprtraceback reprtraceback = exc_repr.reprtraceback
error_message = exc_repr.reprcrash.message # type: ignore error_message = exc_repr.reprcrash.message # type: ignore
error_line = None error_line = None
@@ -69,6 +73,9 @@ class PytestNeotestAdapter(NeotestAdapter):
): ):
error_line = repr.reprfileloc.lineno - 1 error_line = repr.reprfileloc.lineno - 1
errors.append({"message": error_message, "line": error_line}) 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),