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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
"""Utility functions for unit testing."""
import functools
import os
import sys
import tempfile
import types
from contextlib import contextmanager
from textwrap import dedent
__all__ = ["skip", "skipIf", "temporary_file"]
def _id(obj):
return obj
try:
from unittest import skip
except ImportError:
# Provide basic replacement for unittest.skip
def skip(reason):
"""Unconditionally skip a test."""
def decorator(test_item):
if not isinstance(test_item, (type, types.ClassType)):
@functools.wraps(test_item)
def skip_wrapper(*args, **kwds):
if reason:
sys.stderr.write("skipped, %s ... " % reason)
else:
sys.stderr.write("skipped, ")
return
test_item = skip_wrapper
return test_item
return decorator
try:
from unittest import skipIf
except ImportError:
# Provide basic replacement for unittest.skipIf
def skipIf(condition, reason):
"""Skip a test if the condition is true."""
if condition:
return skip(reason)
return _id
@contextmanager
def temporary_file(content=None, mode=None):
tmpf, tmpfname = tempfile.mkstemp()
os.close(tmpf)
if mode is None:
if content is None:
mode = "rb"
else:
mode = "wb"
tmpf = open(tmpfname, mode)
if content is not None:
if isinstance(content, unicode):
tmpf.write(dedent(content).encode("utf8"))
else:
tmpf.write(content)
tmpf.close()
yield tmpfname
os.unlink(tmpfname)
|