Skip to content

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

[test] LogFormatter.error

Eugenio Lacuesta27436cbbc9e7
diff --git a/tests/test_logformatter.py b/tests/test_logformatter.py
index eb9c4a561..502bc4ccc 100644
--- a/tests/test_logformatter.py
+++ b/tests/test_logformatter.py
@@ -23,7 +23,7 @@ class CustomItem(Item):
return "name: %s" % self['name']
-class LoggingContribTest(unittest.TestCase):
+class LoggingFormatterTest(unittest.TestCase):
def setUp(self):
self.formatter = LogFormatter()
@@ -61,6 +61,16 @@ class LoggingContribTest(unittest.TestCase):
lines = logline.splitlines()
assert all(isinstance(x, six.text_type) for x in lines)
self.assertEqual(lines, [u"Dropped: \u2018", '{}'])
+
+ def test_error(self):
+ # In practice, the complete traceback is shown by passing the
+ # 'exc_info' argument to the logging function
+ item = {'key': 'value'}
+ exception = Exception()
+ response = Response("http://www.example.com")
+ logkws = self.formatter.error(item, exception, response, self.spider)
+ logline = logkws['msg'] % logkws['args']
+ self.assertEqual(logline, u"'Error processing {'key': 'value'}'")
def test_scraped(self):
item = CustomItem()
@@ -75,26 +85,30 @@ class LoggingContribTest(unittest.TestCase):
class LogFormatterSubclass(LogFormatter):
def crawled(self, request, response, spider):
- kwargs = super(LogFormatterSubclass, self).crawled(
- request, response, spider)
+ kwargs = super(LogFormatterSubclass, self).crawled(request, response, spider)
CRAWLEDMSG = (
- u"Crawled (%(status)s) %(request)s (referer: "
- u"%(referer)s)%(flags)s"
+ u"Crawled (%(status)s) %(request)s (referer: %(referer)s) %(flags)s"
)
+ log_args = kwargs['args']
+ log_args['flags'] = str(request.flags)
return {
'level': kwargs['level'],
'msg': CRAWLEDMSG,
- 'args': kwargs['args']
+ 'args': log_args,
}
-class LogformatterSubclassTest(LoggingContribTest):
+class LogformatterSubclassTest(unittest.TestCase):
def setUp(self):
self.formatter = LogFormatterSubclass()
self.spider = Spider('default')
def test_flags_in_request(self):
- pass
+ req = Request("http://www.example.com", flags=['test','flag'])
+ res = Response("http://www.example.com")
+ logkws = self.formatter.crawled(req, res, self.spider)
+ logline = logkws['msg'] % logkws['args']
+ self.assertEqual(logline, "Crawled (200) <GET http://www.example.com> (referer: None) ['test', 'flag']")
class SkipMessagesLogFormatter(LogFormatter):