mirror of https://github.com/scrapy/scrapy.git
Trial functionality for running tests with pytest
* Change current dir to tmp dir on each test run * Log twisted with test.log
This commit is contained in:
parent
986be9a396
commit
20b4c8f3ea
|
|
@ -0,0 +1,3 @@
|
|||
|
||||
[pytest]
|
||||
usefixtures = chdir setlog
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
|
||||
import pytest
|
||||
from twisted.python import log
|
||||
|
||||
|
||||
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()
|
||||
Loading…
Reference in New Issue