Scrapy, a fast high-level web crawling & scraping framework for Python.
Ensure contract hook tests stop after failures (#8365)
ethancyyiucd65e2da0556
diff --git a/scrapy/contracts/__init__.py b/scrapy/contracts/__init__.pyindex 76885988a..f384b52cd 100644--- a/scrapy/contracts/__init__.py+++ b/scrapy/contracts/__init__.py@@ -56,13 +56,14 @@ def _run_hook(try:results.startTest(testcase)process(value)- results.stopTest(testcase)except AssertionError:results.addFailure(testcase, sys.exc_info())except Exception:results.addError(testcase, sys.exc_info())else:results.addSuccess(testcase)+ finally:+ results.stopTest(testcase)class Contract:diff --git a/tests/test_contracts.py b/tests/test_contracts.pyindex e1bdc8969..9d4a00dc8 100644--- a/tests/test_contracts.py+++ b/tests/test_contracts.py@@ -1,7 +1,7 @@from __future__ import annotationsfrom typing import TYPE_CHECKING, Any-from unittest import TextTestResult+from unittest import TestCase, TextTestResult, TextTestRunnerimport pytestfrom twisted.python import failure@@ -865,11 +865,20 @@ class PostProcessErrorContract(Contract):raise ValueError("post-process error")+class _TrackingTestResult(TextTestResult):+ def __init__(self) -> None:+ runner = TextTestRunner(descriptions=False, verbosity=0)+ super().__init__(runner.stream, runner.descriptions, runner.verbosity)+ self.stopped_tests: list[TestCase] = []++ def stopTest(self, test: TestCase) -> None:+ self.stopped_tests.append(test)+ super().stopTest(test)++class TestCustomContractPrePostProcess:def setup_method(self):- self.results = TextTestResult( # type: ignore[type-var]- stream=None, descriptions=False, verbosity=0- )+ self.results = _TrackingTestResult()def test_pre_hook_keyboard_interrupt(self):spider = DemoSpider()@@ -884,6 +893,7 @@ class TestCustomContractPrePostProcess:assert not self.results.failuresassert not self.results.errors+ assert self.results.stopped_tests.count(contract.testcase_pre) == 1def test_post_hook_keyboard_interrupt(self):spider = DemoSpider()@@ -898,6 +908,7 @@ class TestCustomContractPrePostProcess:assert not self.results.failuresassert not self.results.errors+ assert self.results.stopped_tests.count(contract.testcase_post) == 1def test_pre_hook_success(self):spider = DemoSpider()@@ -911,6 +922,7 @@ class TestCustomContractPrePostProcess:assert not self.results.failuresassert not self.results.errors+ assert self.results.stopped_tests.count(contract.testcase_pre) == 1def test_pre_hook_assertion_failure(self):spider = DemoSpider()@@ -924,6 +936,7 @@ class TestCustomContractPrePostProcess:assert self.results.failuresassert not self.results.errors+ assert self.results.stopped_tests.count(contract.testcase_pre) == 1def test_pre_hook_error(self):spider = DemoSpider()@@ -936,6 +949,7 @@ class TestCustomContractPrePostProcess:_call(request, response)assert self.results.errors+ assert self.results.stopped_tests.count(contract.testcase_pre) == 1@coroutine_testasync def test_pre_hook_async_callback(self):@@ -990,6 +1004,7 @@ class TestCustomContractPrePostProcess:_call(request, response)assert self.results.errors+ assert self.results.stopped_tests.count(contract.testcase_post) == 1class LegacyHookContract(Contract):