1
from cStringIO import StringIO
9
class TestHelper(unittest.TestCase):
14
self._helper_instances = []
15
for helper_factory in self.helpers:
16
helper = helper_factory()
18
self._helper_instances.append(helper)
21
for helper in reversed(self._helper_instances):
22
helper.tear_down(self)
25
class MakePath(object):
27
def set_up(self, test_case):
28
self.dirname = tempfile.mkdtemp()
31
test_case.make_dir = self.make_dir
32
test_case.make_path = self.make_path
34
def tear_down(self, test_case):
35
shutil.rmtree(self.dirname)
36
[shutil.rmtree(dir) for dir in self.dirs]
39
path = tempfile.mkdtemp()
40
self.dirs.append(path)
43
def make_path(self, content=None, path=None):
46
path = "%s/%03d" % (self.dirname, self.counter)
47
if content is not None:
48
file = open(path, "w")
56
class LogKeeper(object):
57
"""Record logging information.
59
Puts a 'logfile' attribute on your test case, which is a StringIO
60
containing all log output.
63
def set_up(self, test_case):
64
logger = logging.getLogger()
65
test_case.logfile = StringIO()
66
handler = logging.StreamHandler(test_case.logfile)
67
self.old_handlers = logger.handlers
68
# Sanity check; this might not be 100% what we want
70
test_case.assertEquals(len(self.old_handlers), 1)
71
test_case.assertEquals(self.old_handlers[0].stream, sys.stderr)
72
logger.handlers = [handler]
74
def tear_down(self, test_case):
75
logging.getLogger().handlers = self.old_handlers