forked from scrapy/scrapy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conftest.py
50 lines (37 loc) · 1.33 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import six
import pytest
from twisted.python import log
from scrapy import optional_features
collect_ignore = ["scrapy/stats.py", "scrapy/project.py"]
if 'django' not in optional_features:
collect_ignore.append("tests/test_djangoitem/models.py")
if six.PY3:
for line in open('tests/py3-ignores.txt'):
file_path = line.strip()
if len(file_path) > 0 and file_path[0] != '#':
collect_ignore.append(file_path)
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()