mirror of https://github.com/scrapy/scrapy.git
[Tests] Monkey patch unittest.TestCase to prevent DeprecationWarning(s)
This commit is contained in:
parent
fa6a0d799b
commit
09e27d2d2e
|
|
@ -35,3 +35,16 @@ def get_testdata(*paths):
|
|||
path = os.path.join(tests_datadir, *paths)
|
||||
with open(path, 'rb') as f:
|
||||
return f.read()
|
||||
|
||||
|
||||
# FIXME: delete after dropping py2 support
|
||||
# Monkey patch the unittest module to prevent the
|
||||
# DeprecationWarning about assertRaisesRegexp -> assertRaisesRegex
|
||||
import sys
|
||||
if sys.version_info[0] == 2:
|
||||
import unittest
|
||||
import twisted.trial.unittest
|
||||
if not getattr(unittest.TestCase, 'assertRaisesRegex', None):
|
||||
unittest.TestCase.assertRaisesRegex = unittest.TestCase.assertRaisesRegexp
|
||||
if not getattr(twisted.trial.unittest.TestCase, 'assertRaisesRegex', None):
|
||||
twisted.trial.unittest.TestCase.assertRaisesRegex = twisted.trial.unittest.TestCase.assertRaisesRegexp
|
||||
|
|
|
|||
|
|
@ -103,7 +103,7 @@ class PythonItemExporterTest(BaseItemExporterTest):
|
|||
return PythonItemExporter(binary=False, **kwargs)
|
||||
|
||||
def test_invalid_option(self):
|
||||
with self.assertRaisesRegexp(TypeError, "Unexpected options: invalid_option"):
|
||||
with self.assertRaisesRegex(TypeError, "Unexpected options: invalid_option"):
|
||||
PythonItemExporter(invalid_option='something')
|
||||
|
||||
def test_nested_item(self):
|
||||
|
|
|
|||
|
|
@ -147,11 +147,11 @@ class HeadersTest(unittest.TestCase):
|
|||
self.assertEqual(h1.getlist('hey'), [b'5'])
|
||||
|
||||
def test_invalid_value(self):
|
||||
self.assertRaisesRegexp(TypeError, 'Unsupported value type',
|
||||
Headers, {'foo': object()})
|
||||
self.assertRaisesRegexp(TypeError, 'Unsupported value type',
|
||||
Headers().__setitem__, 'foo', object())
|
||||
self.assertRaisesRegexp(TypeError, 'Unsupported value type',
|
||||
Headers().setdefault, 'foo', object())
|
||||
self.assertRaisesRegexp(TypeError, 'Unsupported value type',
|
||||
Headers().setlist, 'foo', [object()])
|
||||
self.assertRaisesRegex(TypeError, 'Unsupported value type',
|
||||
Headers, {'foo': object()})
|
||||
self.assertRaisesRegex(TypeError, 'Unsupported value type',
|
||||
Headers().__setitem__, 'foo', object())
|
||||
self.assertRaisesRegex(TypeError, 'Unsupported value type',
|
||||
Headers().setdefault, 'foo', object())
|
||||
self.assertRaisesRegex(TypeError, 'Unsupported value type',
|
||||
Headers().setlist, 'foo', [object()])
|
||||
|
|
|
|||
|
|
@ -989,9 +989,9 @@ class FormRequestTest(RequestTest):
|
|||
|
||||
xpath = u"//form[@name='\u03b1']"
|
||||
encoded = xpath if six.PY3 else xpath.encode('unicode_escape')
|
||||
self.assertRaisesRegexp(ValueError, re.escape(encoded),
|
||||
self.request_class.from_response,
|
||||
response, formxpath=xpath)
|
||||
self.assertRaisesRegex(ValueError, re.escape(encoded),
|
||||
self.request_class.from_response,
|
||||
response, formxpath=xpath)
|
||||
|
||||
def test_from_response_button_submit(self):
|
||||
response = _buildresponse(
|
||||
|
|
|
|||
|
|
@ -135,9 +135,9 @@ class BaseResponseTest(unittest.TestCase):
|
|||
r = self.response_class("http://example.com", body=b'hello')
|
||||
if self.response_class == Response:
|
||||
msg = "Response content isn't text"
|
||||
self.assertRaisesRegexp(AttributeError, msg, getattr, r, 'text')
|
||||
self.assertRaisesRegexp(NotSupported, msg, r.css, 'body')
|
||||
self.assertRaisesRegexp(NotSupported, msg, r.xpath, '//body')
|
||||
self.assertRaisesRegex(AttributeError, msg, getattr, r, 'text')
|
||||
self.assertRaisesRegex(NotSupported, msg, r.css, 'body')
|
||||
self.assertRaisesRegex(NotSupported, msg, r.xpath, '//body')
|
||||
else:
|
||||
r.text
|
||||
r.css('body')
|
||||
|
|
@ -425,13 +425,13 @@ class TextResponseTest(BaseResponseTest):
|
|||
|
||||
def test_follow_selector_list(self):
|
||||
resp = self._links_response()
|
||||
self.assertRaisesRegexp(ValueError, 'SelectorList',
|
||||
resp.follow, resp.css('a'))
|
||||
self.assertRaisesRegex(ValueError, 'SelectorList',
|
||||
resp.follow, resp.css('a'))
|
||||
|
||||
def test_follow_selector_invalid(self):
|
||||
resp = self._links_response()
|
||||
self.assertRaisesRegexp(ValueError, 'Unsupported',
|
||||
resp.follow, resp.xpath('count(//div)')[0])
|
||||
self.assertRaisesRegex(ValueError, 'Unsupported',
|
||||
resp.follow, resp.xpath('count(//div)')[0])
|
||||
|
||||
def test_follow_selector_attribute(self):
|
||||
resp = self._links_response()
|
||||
|
|
@ -443,8 +443,8 @@ class TextResponseTest(BaseResponseTest):
|
|||
url='http://example.com',
|
||||
body=b'<html><body><a name=123>click me</a></body></html>',
|
||||
)
|
||||
self.assertRaisesRegexp(ValueError, 'no href',
|
||||
resp.follow, resp.css('a')[0])
|
||||
self.assertRaisesRegex(ValueError, 'no href',
|
||||
resp.follow, resp.css('a')[0])
|
||||
|
||||
def test_follow_whitespace_selector(self):
|
||||
resp = self.response_class(
|
||||
|
|
|
|||
|
|
@ -85,5 +85,5 @@ class SelectorTestCase(unittest.TestCase):
|
|||
x.__class__.__name__
|
||||
|
||||
def test_selector_bad_args(self):
|
||||
with self.assertRaisesRegexp(ValueError, 'received both response and text'):
|
||||
with self.assertRaisesRegex(ValueError, 'received both response and text'):
|
||||
Selector(TextResponse(url='http://example.com', body=b''), text=u'')
|
||||
|
|
|
|||
|
|
@ -595,5 +595,5 @@ class NoParseMethodSpiderTest(unittest.TestCase):
|
|||
resp = TextResponse(url="http://www.example.com/random_url", body=text)
|
||||
|
||||
exc_msg = 'Spider.parse callback is not defined'
|
||||
with self.assertRaisesRegexp(NotImplementedError, exc_msg):
|
||||
with self.assertRaisesRegex(NotImplementedError, exc_msg):
|
||||
spider.parse(resp)
|
||||
|
|
|
|||
|
|
@ -84,8 +84,8 @@ class SpiderLoaderTest(unittest.TestCase):
|
|||
module = 'tests.test_spiderloader.test_spiders.spider1'
|
||||
runner = CrawlerRunner({'SPIDER_MODULES': [module]})
|
||||
|
||||
self.assertRaisesRegexp(KeyError, 'Spider not found',
|
||||
runner.create_crawler, 'spider2')
|
||||
self.assertRaisesRegex(KeyError, 'Spider not found',
|
||||
runner.create_crawler, 'spider2')
|
||||
|
||||
crawler = runner.create_crawler('spider1')
|
||||
self.assertTrue(issubclass(crawler.spidercls, scrapy.Spider))
|
||||
|
|
|
|||
Loading…
Reference in New Issue