diff --git a/.travis.yml b/.travis.yml index 367a56280..08ca870a5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,9 +5,11 @@ env: - TOXENV=precise - TOXENV=trunk - TOXENV=pypy +- TOXENV=py33 matrix: allow_failures: - env: TOXENV=pypy + - env: TOXENV=py33 install: - ./.travis-workarounds.sh - pip install tox diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 000000000..8d3bc91a5 --- /dev/null +++ b/pytest.ini @@ -0,0 +1,5 @@ + +[pytest] +usefixtures = chdir setlog +python_files=test_*.py __init__.py +addopts = --doctest-modules diff --git a/scrapy/conftest.py b/scrapy/conftest.py new file mode 100644 index 000000000..7e11f331f --- /dev/null +++ b/scrapy/conftest.py @@ -0,0 +1,45 @@ + +import pytest +from twisted.python import log + +from scrapy import optional_features + +collect_ignore = ["stats.py"] +if 'django' not in optional_features: + collect_ignore.append("tests/test_djangoitem/models.py") + + +class LogObservers: + """Class for keeping track of log observers across test modules""" + + def __init__(self): + self.observers = [] + + def add(self, logfile='test.log'): + fileobj = open(logfile, 'wb') + observer = log.FileLogObserver(fileobj) + log.startLoggingWithObserver(observer.emit, 0) + self.observers.append((fileobj, observer)) + + def remove(self): + fileobj, observer = self.observers.pop() + log.removeObserver(observer.emit) + fileobj.close() + + +@pytest.fixture(scope='module') +def log_observers(): + return LogObservers() + + +@pytest.fixture() +def setlog(request, log_observers): + """Attach test.log file observer to twisted log, for trial compatibility""" + log_observers.add() + request.addfinalizer(log_observers.remove) + + +@pytest.fixture() +def chdir(tmpdir): + """Change to pytest-provided temporary directory""" + tmpdir.chdir() diff --git a/scrapy/contrib/httpcache.py b/scrapy/contrib/httpcache.py index 5dee898f0..fefebc41c 100644 --- a/scrapy/contrib/httpcache.py +++ b/scrapy/contrib/httpcache.py @@ -290,10 +290,11 @@ def parse_cachecontrol(header): http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9 - >>> parse_cachecontrol('public, max-age=3600') - {'public': None, 'max-age': '3600'} - >>> parse_cachecontrol('') - {} + >>> parse_cachecontrol('public, max-age=3600') == {'public': None, + ... 'max-age': '3600'} + True + >>> parse_cachecontrol('') == {} + True """ directives = {} diff --git a/scrapy/tests/test_spidermanager/__init__.py b/scrapy/tests/test_spidermanager/__init__.py index c8697d3b5..eebd6b298 100644 --- a/scrapy/tests/test_spidermanager/__init__.py +++ b/scrapy/tests/test_spidermanager/__init__.py @@ -27,6 +27,7 @@ class SpiderManagerTest(unittest.TestCase): def tearDown(self): del self.spiderman + del sys.modules['test_spiders_xxx'] sys.path.remove(self.tmpdir) def test_interface(self): diff --git a/scrapy/tests/test_squeue.py b/scrapy/tests/test_squeue.py index a3c64db8d..83ffcc4b7 100644 --- a/scrapy/tests/test_squeue.py +++ b/scrapy/tests/test_squeue.py @@ -7,12 +7,12 @@ from scrapy.contrib.loader import ItemLoader class TestItem(Item): name = Field() -def test_processor(x): +def _test_procesor(x): return x + x class TestLoader(ItemLoader): default_item_class = TestItem - name_out = staticmethod(test_processor) + name_out = staticmethod(_test_procesor) class MarshalFifoDiskQueueTest(t.FifoDiskQueueTest): diff --git a/tests-requirements.txt b/tests-requirements.txt index 34275dfa7..b7d6a0a56 100644 --- a/tests-requirements.txt +++ b/tests-requirements.txt @@ -1,3 +1,4 @@ # Tests requirements mock mitmproxy >= 0.10 +pytest-twisted diff --git a/tox.ini b/tox.ini index cebffbccc..597abfb5d 100644 --- a/tox.ini +++ b/tox.ini @@ -5,6 +5,8 @@ [tox] envlist = py27, pypy, precise, trunk, py33 +indexserver = + HPK = https://devpi.net/hpk/dev/ [testenv] deps = @@ -15,7 +17,7 @@ deps = django -rtests-requirements.txt commands = - trial {posargs:scrapy} + py.test --twisted {posargs:scrapy} [testenv:precise] basepython = python2.7 @@ -34,7 +36,7 @@ basepython = python2.7 commands = pip install https://github.com/scrapy/w3lib/archive/master.zip#egg=w3lib pip install https://github.com/scrapy/queuelib/archive/master.zip#egg=queuelib - trial {posargs:scrapy} + py.test --twisted {posargs:scrapy} [testenv:py33] basepython = python3.3 @@ -45,8 +47,10 @@ deps = cssselect>=0.9 queuelib>=1.1.1 w3lib>=1.5 -commands = - trial {posargs:scrapy} + # tests requirements + mock + :HPK:pytest>2.5.2 + pytest-twisted [testenv:windows] commands =