Update tests affected by SPIDER_LOADER_REQUIRE_NAME

This commit is contained in:
Adrián Chaves 2020-02-12 19:42:10 +01:00
parent 182282b2e0
commit 7aafb76c50
3 changed files with 24 additions and 7 deletions

View File

@ -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)

View File

@ -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))

View File

@ -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)