Skip to content

Scrapy, a fast high-level web crawling & scraping framework for Python.

Merge branch 'master' into master

Andrey Rakhmatullin5bf0e1d1db97
diff --cc tests/test_command_check.py
index 70098a4e7,d503628b8..effafae54
--- a/tests/test_command_check.py
+++ b/tests/test_command_check.py
@@@ -1,6 -1,8 +1,8 @@@
- from unittest.mock import Mock, call, patch
+ import sys
+ from io import StringIO
-from unittest.mock import Mock, PropertyMock, patch
++from unittest.mock import Mock, call, PropertyMock, patch
- from scrapy.commands.check import TextTestResult
-from scrapy.commands.check import Command
++from scrapy.commands.check import Command, TextTestResult
from tests.test_commands import CommandTest
@@@ -98,51 -100,52 +100,101 @@@ class CheckSpider(scrapy.Spider)
"""
self._test_contract(parse_def=parse_def)
+ def test_printSummary_with_unsuccessful_test_result_without_errors_and_without_failures(
+ self,
+ ):
+ result = TextTestResult(Mock(), descriptions=False, verbosity=1)
+ start_time = 1.0
+ stop_time = 2.0
+ result.testsRun = 5
+ result.failures = []
+ result.errors = []
+ result.unexpectedSuccesses = ["a", "b"]
+ with patch.object(result.stream, "write") as mock_write:
+ result.printSummary(start_time, stop_time)
+ mock_write.assert_has_calls([call("FAILED"), call("\n")])
+
+ def test_printSummary_with_unsuccessful_test_result_with_only_failures(self):
+ result = TextTestResult(Mock(), descriptions=False, verbosity=1)
+ start_time = 1.0
+ stop_time = 2.0
+ result.testsRun = 5
+ result.failures = [(self, "failure")]
+ result.errors = []
+ with patch.object(result.stream, "writeln") as mock_write:
+ result.printSummary(start_time, stop_time)
+ mock_write.assert_called_with(" (failures=1)")
+
+ def test_printSummary_with_unsuccessful_test_result_with_only_errors(self):
+ result = TextTestResult(Mock(), descriptions=False, verbosity=1)
+ start_time = 1.0
+ stop_time = 2.0
+ result.testsRun = 5
+ result.failures = []
+ result.errors = [(self, "error")]
+ with patch.object(result.stream, "writeln") as mock_write:
+ result.printSummary(start_time, stop_time)
+ mock_write.assert_called_with(" (errors=1)")
+
+ def test_printSummary_with_unsuccessful_test_result_with_both_failures_and_errors(
+ self,
+ ):
+ result = TextTestResult(Mock(), descriptions=False, verbosity=1)
+ start_time = 1.0
+ stop_time = 2.0
+ result.testsRun = 5
+ result.failures = [(self, "failure")]
+ result.errors = [(self, "error")]
+ with patch.object(result.stream, "writeln") as mock_write:
+ result.printSummary(start_time, stop_time)
+ mock_write.assert_called_with(" (failures=1, errors=1)")
++
+ @patch("scrapy.commands.check.ContractsManager")
+ def test_run_with_opts_list_prints_spider(self, cm_cls_mock):
+ output = StringIO()
+ sys.stdout = output
+ cmd = Command()
+ cmd.settings = Mock(getwithbase=Mock(return_value={}))
+ cm_cls_mock.return_value = cm_mock = Mock()
+ spider_loader_mock = Mock()
+ cmd.crawler_process = Mock(spider_loader=spider_loader_mock)
+ spider_name = "FakeSpider"
+ spider_cls_mock = Mock()
+ type(spider_cls_mock).name = PropertyMock(return_value=spider_name)
+ spider_loader_mock.load.side_effect = lambda x: {spider_name: spider_cls_mock}[
+ x
+ ]
+ tested_methods = ["fakeMethod1", "fakeMethod2"]
+ cm_mock.tested_methods_from_spidercls.side_effect = lambda x: {
+ spider_cls_mock: tested_methods
+ }[x]
+
+ cmd.run([spider_name], Mock(list=True))
+
+ self.assertEqual(
+ "FakeSpider\n * fakeMethod1\n * fakeMethod2\n", output.getvalue()
+ )
+ sys.stdout = sys.__stdout__
+
+ @patch("scrapy.commands.check.ContractsManager")
+ def test_run_without_opts_list_does_not_crawl_spider_with_no_tested_methods(
+ self, cm_cls_mock
+ ):
+ cmd = Command()
+ cmd.settings = Mock(getwithbase=Mock(return_value={}))
+ cm_cls_mock.return_value = cm_mock = Mock()
+ spider_loader_mock = Mock()
+ cmd.crawler_process = Mock(spider_loader=spider_loader_mock)
+ spider_name = "FakeSpider"
+ spider_cls_mock = Mock()
+ spider_loader_mock.load.side_effect = lambda x: {spider_name: spider_cls_mock}[
+ x
+ ]
+ tested_methods = []
+ cm_mock.tested_methods_from_spidercls.side_effect = lambda x: {
+ spider_cls_mock: tested_methods
+ }[x]
+
+ cmd.run([spider_name], Mock(list=False))
+
+ cmd.crawler_process.crawl.assert_not_called()