1
0
mirror of https://github.com/scrapy/scrapy.git synced 2025-02-26 14:24:27 +00:00

Use pytest.mark.parametrize decorator

This commit is contained in:
Paul Tremberth 2016-01-22 18:22:19 +01:00
parent 61ca8d587a
commit 1a30a7774b

View File

@ -1,5 +1,7 @@
from os.path import join
import pytest
from twisted.trial import unittest
from twisted.internet import defer
@ -10,63 +12,6 @@ from scrapy.utils.testproc import ProcessTest
from tests import tests_datadir
class ShellURLTest(unittest.TestCase):
pass
def create_guess_scheme_t(args):
def do_expected(self):
url = guess_scheme(args[0])
assert url.startswith(args[1]), \
'Wrong scheme guessed: for `%s` got `%s`, expected `%s...`' % (
args[0], url, args[1])
return do_expected
def create_skipped_scheme_t(args):
def do_expected(self):
raise unittest.SkipTest(args[2])
url = guess_scheme(args[0])
assert url.startswith(args[1])
return do_expected
for k, args in enumerate ([
('/index', 'file://'),
('/index.html', 'file://'),
('./index.html', 'file://'),
('../index.html', 'file://'),
('../../index.html', 'file://'),
('./data/index.html', 'file://'),
('.hidden/data/index.html', 'file://'),
('/home/user/www/index.html', 'file://'),
('//home/user/www/index.html', 'file://'),
('file:///home/user/www/index.html', 'file://'),
('index.html', 'http://'),
('example.com', 'http://'),
('www.example.com', 'http://'),
('www.example.com/index.html', 'http://'),
('http://example.com', 'http://'),
('http://example.com/index.html', 'http://'),
('localhost', 'http://'),
('localhost/index.html', 'http://'),
# some corner cases (default to http://)
('/', 'http://'),
('.../test', 'http://'),
], start=1):
t_method = create_guess_scheme_t(args)
t_method.__name__ = 'test_uri_%03d' % k
setattr (ShellURLTest, t_method.__name__, t_method)
# TODO: the following tests do not pass with current implementation
for k, args in enumerate ([
('C:\absolute\path\to\a\file.html', 'file://',
'Windows filepath are not supported for scrapy shell'),
], start=1):
t_method = create_skipped_scheme_t(args)
t_method.__name__ = 'test_uri_skipped_%03d' % k
setattr (ShellURLTest, t_method.__name__, t_method)
class ShellTest(ProcessTest, SiteTest, unittest.TestCase):
@ -136,3 +81,40 @@ class ShellTest(ProcessTest, SiteTest, unittest.TestCase):
check_code=False)
self.assertEqual(errcode, 1, out or err)
self.assertIn(b'DNS lookup failed', err)
@pytest.mark.parametrize("url, scheme", [
('/index', 'file://'),
('/index.html', 'file://'),
('./index.html', 'file://'),
('../index.html', 'file://'),
('../../index.html', 'file://'),
('./data/index.html', 'file://'),
('.hidden/data/index.html', 'file://'),
('/home/user/www/index.html', 'file://'),
('//home/user/www/index.html', 'file://'),
('file:///home/user/www/index.html', 'file://'),
('index.html', 'http://'),
('example.com', 'http://'),
('www.example.com', 'http://'),
('www.example.com/index.html', 'http://'),
('http://example.com', 'http://'),
('http://example.com/index.html', 'http://'),
('localhost', 'http://'),
('localhost/index.html', 'http://'),
# some corner cases (default to http://)
('/', 'http://'),
('.../test', 'http://'),
pytest.mark.xfail(
(r'C:\absolute\path\to\a\file.html', 'file://'),
reason = 'Windows filepath are not supported for scrapy shell'
),
])
def test_guess_scheme(url, scheme):
guessed_url = guess_scheme(url)
assert guessed_url.startswith(scheme), \
'Wrong scheme guessed: for `%s` got `%s`, expected `%s...`' % (
url, guessed_url, scheme)