Skip to content

Commit 20b4c8f

Browse files
committed
Trial functionality for running tests with pytest
* Change current dir to tmp dir on each test run * Log twisted with test.log
1 parent 986be9a commit 20b4c8f

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

pytest.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
[pytest]
3+
usefixtures = chdir setlog

scrapy/conftest.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
import pytest
3+
from twisted.python import log
4+
5+
6+
class LogObservers:
7+
"""Class for keeping track of log observers across test modules"""
8+
9+
def __init__(self):
10+
self.observers = []
11+
12+
def add(self, logfile='test.log'):
13+
fileobj = open(logfile, 'wb')
14+
observer = log.FileLogObserver(fileobj)
15+
log.startLoggingWithObserver(observer.emit, 0)
16+
self.observers.append((fileobj, observer))
17+
18+
def remove(self):
19+
fileobj, observer = self.observers.pop()
20+
log.removeObserver(observer.emit)
21+
fileobj.close()
22+
23+
24+
@pytest.fixture(scope='module')
25+
def log_observers():
26+
return LogObservers()
27+
28+
29+
@pytest.fixture()
30+
def setlog(request, log_observers):
31+
"""Attach test.log file observer to twisted log, for trial compatibility"""
32+
log_observers.add()
33+
request.addfinalizer(log_observers.remove)
34+
35+
36+
@pytest.fixture()
37+
def chdir(tmpdir):
38+
"""Change to pytest-provided temporary directory"""
39+
tmpdir.chdir()

0 commit comments

Comments
 (0)