mirror of https://github.com/scrapy/scrapy.git
Merge pull request #4663 from ajaymittur28/scrapy-check-test
Add Tests to `scrapy check` command
This commit is contained in:
commit
0cf1340c29
|
|
@ -40,3 +40,4 @@ flake8-ignore =
|
|||
scrapy/utils/multipart.py F403
|
||||
scrapy/utils/url.py F403 F405
|
||||
tests/test_loader.py E741
|
||||
|
||||
|
|
|
|||
|
|
@ -78,19 +78,19 @@ class Command(ScrapyCommand):
|
|||
elif tested_methods:
|
||||
self.crawler_process.crawl(spidercls)
|
||||
|
||||
# start checks
|
||||
if opts.list:
|
||||
for spider, methods in sorted(contract_reqs.items()):
|
||||
if not methods and not opts.verbose:
|
||||
continue
|
||||
print(spider)
|
||||
for method in sorted(methods):
|
||||
print(' * %s' % method)
|
||||
else:
|
||||
start = time.time()
|
||||
self.crawler_process.start()
|
||||
stop = time.time()
|
||||
# start checks
|
||||
if opts.list:
|
||||
for spider, methods in sorted(contract_reqs.items()):
|
||||
if not methods and not opts.verbose:
|
||||
continue
|
||||
print(spider)
|
||||
for method in sorted(methods):
|
||||
print(' * %s' % method)
|
||||
else:
|
||||
start = time.time()
|
||||
self.crawler_process.start()
|
||||
stop = time.time()
|
||||
|
||||
result.printErrors()
|
||||
result.printSummary(start, stop)
|
||||
self.exitcode = int(not result.wasSuccessful())
|
||||
result.printErrors()
|
||||
result.printSummary(start, stop)
|
||||
self.exitcode = int(not result.wasSuccessful())
|
||||
|
|
|
|||
|
|
@ -0,0 +1,97 @@
|
|||
from os.path import join, abspath
|
||||
|
||||
from tests.test_commands import CommandTest
|
||||
|
||||
|
||||
class CheckCommandTest(CommandTest):
|
||||
|
||||
command = 'check'
|
||||
|
||||
def setUp(self):
|
||||
super(CheckCommandTest, self).setUp()
|
||||
self.spider_name = 'check_spider'
|
||||
self.spider = abspath(join(self.proj_mod_path, 'spiders', 'checkspider.py'))
|
||||
|
||||
def _write_contract(self, contracts, parse_def):
|
||||
with open(self.spider, 'w') as file:
|
||||
file.write("""
|
||||
import scrapy
|
||||
|
||||
class CheckSpider(scrapy.Spider):
|
||||
name = '{0}'
|
||||
start_urls = ['http://example.com']
|
||||
|
||||
def parse(self, response, **cb_kwargs):
|
||||
\"\"\"
|
||||
@url http://example.com
|
||||
{1}
|
||||
\"\"\"
|
||||
{2}
|
||||
""".format(self.spider_name, contracts, parse_def))
|
||||
|
||||
def _test_contract(self, contracts='', parse_def='pass'):
|
||||
self._write_contract(contracts, parse_def)
|
||||
p, out, err = self.proc('check')
|
||||
self.assertNotIn('F', out)
|
||||
self.assertIn('OK', err)
|
||||
self.assertEqual(p.returncode, 0)
|
||||
|
||||
def test_check_returns_requests_contract(self):
|
||||
contracts = """
|
||||
@returns requests 1
|
||||
"""
|
||||
parse_def = """
|
||||
yield scrapy.Request(url='http://next-url.com')
|
||||
"""
|
||||
self._test_contract(contracts, parse_def)
|
||||
|
||||
def test_check_returns_items_contract(self):
|
||||
contracts = """
|
||||
@returns items 1
|
||||
"""
|
||||
parse_def = """
|
||||
yield {'key1': 'val1', 'key2': 'val2'}
|
||||
"""
|
||||
self._test_contract(contracts, parse_def)
|
||||
|
||||
def test_check_cb_kwargs_contract(self):
|
||||
contracts = """
|
||||
@cb_kwargs {"arg1": "val1", "arg2": "val2"}
|
||||
"""
|
||||
parse_def = """
|
||||
if len(cb_kwargs.items()) == 0:
|
||||
raise Exception("Callback args not set")
|
||||
"""
|
||||
self._test_contract(contracts, parse_def)
|
||||
|
||||
def test_check_scrapes_contract(self):
|
||||
contracts = """
|
||||
@scrapes key1 key2
|
||||
"""
|
||||
parse_def = """
|
||||
yield {'key1': 'val1', 'key2': 'val2'}
|
||||
"""
|
||||
self._test_contract(contracts, parse_def)
|
||||
|
||||
def test_check_all_default_contracts(self):
|
||||
contracts = """
|
||||
@returns items 1
|
||||
@returns requests 1
|
||||
@scrapes key1 key2
|
||||
@cb_kwargs {"arg1": "val1", "arg2": "val2"}
|
||||
"""
|
||||
parse_def = """
|
||||
yield {'key1': 'val1', 'key2': 'val2'}
|
||||
yield scrapy.Request(url='http://next-url.com')
|
||||
if len(cb_kwargs.items()) == 0:
|
||||
raise Exception("Callback args not set")
|
||||
"""
|
||||
self._test_contract(contracts, parse_def)
|
||||
|
||||
def test_SCRAPY_CHECK_set(self):
|
||||
parse_def = """
|
||||
import os
|
||||
if not os.environ.get('SCRAPY_CHECK'):
|
||||
raise Exception('SCRAPY_CHECK not set')
|
||||
"""
|
||||
self._test_contract(parse_def=parse_def)
|
||||
Loading…
Reference in New Issue