Skip to content

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

[Tests] Monkey patch unittest.TestCase to prevent DeprecationWarning(s)

Eugenio Lacuesta09e27d2d2e04
diff --git a/tests/__init__.py b/tests/__init__.py
index a54367f8c..e55c77734 100644
--- a/tests/__init__.py
+++ b/tests/__init__.py
@@ -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
diff --git a/tests/test_exporters.py b/tests/test_exporters.py
index cd72c661a..0046c5666 100644
--- a/tests/test_exporters.py
+++ b/tests/test_exporters.py
@@ -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):
diff --git a/tests/test_http_headers.py b/tests/test_http_headers.py
index 504937295..69d906fbf 100644
--- a/tests/test_http_headers.py
+++ b/tests/test_http_headers.py
@@ -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()])
diff --git a/tests/test_http_request.py b/tests/test_http_request.py
index 53d18d4aa..952e208de 100644
--- a/tests/test_http_request.py
+++ b/tests/test_http_request.py
@@ -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(
diff --git a/tests/test_http_response.py b/tests/test_http_response.py
index 3b90e3dac..cd5c3486e 100644
--- a/tests/test_http_response.py
+++ b/tests/test_http_response.py
@@ -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(
diff --git a/tests/test_selector.py b/tests/test_selector.py
index 210e3256c..b2565dd78 100644
--- a/tests/test_selector.py
+++ b/tests/test_selector.py
@@ -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'')
diff --git a/tests/test_spider.py b/tests/test_spider.py
index 1197ea653..e81e6d5f9 100644
--- a/tests/test_spider.py
+++ b/tests/test_spider.py
@@ -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)
diff --git a/tests/test_spiderloader/__init__.py b/tests/test_spiderloader/__init__.py
index 1cd59b99a..106da798c 100644
--- a/tests/test_spiderloader/__init__.py
+++ b/tests/test_spiderloader/__init__.py
@@ -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))