diff --git a/tests/__init__.py b/tests/__init__.py index 12ce79fa9..0eed7f209 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -25,6 +25,16 @@ tests_datadir = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'sample_data') +# Settings that, while not part of the default settings for backward +# compatibility reasons, are encouraged in the documentation. +# +# Not using these settings can cause some backward-compatibility warnings to be +# logged, breaking tests that check logged warnings. +FUTURE_PROOF_SETTINGS = { + 'SPIDER_LOADER_REQUIRE_NAME': False, +} + + def get_testdata(*paths): """Return test data""" path = os.path.join(tests_datadir, *paths) diff --git a/tests/test_crawler.py b/tests/test_crawler.py index f8fa26def..6bc7f055d 100644 --- a/tests/test_crawler.py +++ b/tests/test_crawler.py @@ -20,6 +20,8 @@ from scrapy.extensions.throttle import AutoThrottle from scrapy.extensions import telnet from scrapy.utils.test import get_testenv +from tests import FUTURE_PROOF_SETTINGS + class BaseCrawlerTest(unittest.TestCase): @@ -31,7 +33,7 @@ class BaseCrawlerTest(unittest.TestCase): class CrawlerTestCase(BaseCrawlerTest): def setUp(self): - self.crawler = Crawler(DefaultSpider, Settings()) + self.crawler = Crawler(DefaultSpider, FUTURE_PROOF_SETTINGS) def test_deprecated_attribute_spiders(self): with warnings.catch_warnings(record=True) as w: @@ -173,7 +175,7 @@ class CrawlerRunnerTestCase(BaseCrawlerTest): def test_deprecated_attribute_spiders(self): with warnings.catch_warnings(record=True) as w: - runner = CrawlerRunner(Settings()) + runner = CrawlerRunner(FUTURE_PROOF_SETTINGS) spiders = runner.spiders self.assertEqual(len(w), 1) self.assertIn("CrawlerRunner.spiders", str(w[0].message)) diff --git a/tests/test_spiderloader/__init__.py b/tests/test_spiderloader/__init__.py index d8be6e277..5c344e3ff 100644 --- a/tests/test_spiderloader/__init__.py +++ b/tests/test_spiderloader/__init__.py @@ -17,6 +17,8 @@ from scrapy.settings import Settings from scrapy.http import Request from scrapy.crawler import CrawlerRunner +from tests import FUTURE_PROOF_SETTINGS + module_dir = os.path.dirname(os.path.abspath(__file__)) @@ -101,7 +103,8 @@ class SpiderLoaderTest(unittest.TestCase): with warnings.catch_warnings(record=True) as w: module = 'tests.test_spiderloader.test_spiders.doesnotexist' - settings = Settings({'SPIDER_MODULES': [module], + settings = Settings({**FUTURE_PROOF_SETTINGS, + 'SPIDER_MODULES': [module], 'SPIDER_LOADER_WARN_ONLY': True}) spider_loader = SpiderLoader.from_settings(settings) self.assertIn("Could not load spiders from module", str(w[0].message)) @@ -133,8 +136,9 @@ class DuplicateSpiderNameLoaderTest(unittest.TestCase): with warnings.catch_warnings(record=True) as w: spider_loader = SpiderLoader.from_settings(self.settings) - self.assertEqual(len(w), 1) - msg = str(w[0].message) + # We ignore the warning about SPIDER_LOADER_REQUIRE_NAME + self.assertEqual(len(w), 2) + msg = str(w[1].message) self.assertIn("several spiders with the same name", msg) self.assertIn("'spider3'", msg) @@ -152,8 +156,9 @@ class DuplicateSpiderNameLoaderTest(unittest.TestCase): with warnings.catch_warnings(record=True) as w: spider_loader = SpiderLoader.from_settings(self.settings) - self.assertEqual(len(w), 1) - msg = str(w[0].message) + # We ignore the warning about SPIDER_LOADER_REQUIRE_NAME + self.assertEqual(len(w), 2) + msg = str(w[1].message) self.assertIn("several spiders with the same name", msg) self.assertIn("'spider1'", msg) self.assertIn("'spider2'", msg)