Only define a testcase name when its more readable.
This commit is contained in:
parent
a23c65e206
commit
602b122c8d
|
@ -50,7 +50,7 @@ Or the lastest development version from Github::
|
|||
And run the tests::
|
||||
|
||||
python setup.py test # or...
|
||||
python tests/runner.py
|
||||
python tests/run_tests.py
|
||||
|
||||
|
||||
Usage
|
||||
|
|
2
setup.py
2
setup.py
|
@ -20,7 +20,7 @@ class BaseCommand(Command):
|
|||
class TestCommand(BaseCommand):
|
||||
description = 'run unit tests'
|
||||
def run(self):
|
||||
errno = subprocess.call([sys.executable, 'tests/runner.py'])
|
||||
errno = subprocess.call([sys.executable, 'tests/run_tests.py'])
|
||||
sys.exit(errno)
|
||||
|
||||
|
||||
|
|
|
@ -20,6 +20,17 @@ from collections import namedtuple, defaultdict
|
|||
Result = namedtuple('Result', ('func', 'name', 'failure'))
|
||||
|
||||
|
||||
def _get_doc_line(obj):
|
||||
doc = ''
|
||||
if obj.__doc__:
|
||||
doc = obj.__doc__.lstrip()
|
||||
idx = doc.find('\n')
|
||||
if idx > 0:
|
||||
doc = doc[:idx]
|
||||
|
||||
return doc
|
||||
|
||||
|
||||
def _exc_name(exception_class):
|
||||
if not inspect.isclass(exception_class):
|
||||
exception_class = exception_class.__class__
|
||||
|
@ -61,10 +72,12 @@ class AssertionObject(object):
|
|||
target_length = len(self._target)
|
||||
|
||||
if target_length > other:
|
||||
raise AssertionError('Higher than desired length: {!r} > {!r}'
|
||||
raise AssertionError(
|
||||
'Higher than desired length: {!r} > {!r}'
|
||||
.format(target_length, other))
|
||||
elif target_length < other:
|
||||
raise AssertionError('Lower than desired length: {!r} < {!r}'
|
||||
raise AssertionError(
|
||||
'Lower than desired length: {!r} < {!r}'
|
||||
.format(target_length, other))
|
||||
|
||||
def diff(self, other):
|
||||
|
@ -114,7 +127,6 @@ class AssertionObject(object):
|
|||
.format(name, _exc_name(e), e))
|
||||
|
||||
|
||||
# A nicer alias.
|
||||
ok = AssertionObject
|
||||
|
||||
|
||||
|
@ -127,6 +139,14 @@ class TestCase(object):
|
|||
if t.startswith('test_'):
|
||||
self._tests.append(self._wrap_test(getattr(self, t)))
|
||||
|
||||
@classmethod
|
||||
def name(cls):
|
||||
name = _get_doc_line(cls)
|
||||
if name:
|
||||
return '{} ({})'.format(name, cls.__name__)
|
||||
else:
|
||||
return cls.__name__
|
||||
|
||||
def add_test(self, func):
|
||||
self._tests.append(self._wrap_test(func))
|
||||
|
||||
|
@ -136,20 +156,13 @@ class TestCase(object):
|
|||
|
||||
try:
|
||||
func()
|
||||
except AssertionError as e:
|
||||
except AssertionError as e: # Expected exception
|
||||
failure = str(e)
|
||||
except Exception as e:
|
||||
except Exception as e: # Unexpected exception
|
||||
failure = ''.join(traceback.format_exception(
|
||||
*sys.exc_info())).strip()
|
||||
|
||||
doc = ''
|
||||
if func.__doc__:
|
||||
doc = func.__doc__.lstrip()
|
||||
idx = doc.find('\n')
|
||||
if idx > 0:
|
||||
doc = doc[:idx]
|
||||
|
||||
return Result(func.__name__, doc or None, failure)
|
||||
return Result(func.__name__, _get_doc_line(func) or None, failure)
|
||||
|
||||
return catch_exception
|
||||
|
||||
|
@ -177,8 +190,7 @@ def runner(testcases, setup_func=None, teardown_func=None, config={}):
|
|||
for testcase in testcases:
|
||||
tests = testcase(config)
|
||||
|
||||
print('>> {}'.format(tests.name if hasattr(tests, 'name')
|
||||
else testcase.__name__))
|
||||
print('>> {}'.format(testcase.name()))
|
||||
|
||||
for result in tests.run():
|
||||
name = result.name or result.func
|
||||
|
|
|
@ -45,7 +45,8 @@ def is_test(n):
|
|||
|
||||
|
||||
def get_tests(module):
|
||||
return inspect.getmembers(module, is_test)
|
||||
return [(testcase.name(), testcase) \
|
||||
for _, testcase in inspect.getmembers(module, is_test)]
|
||||
|
||||
|
||||
def run_tests(tests, include=[], exclude=[]):
|
|
@ -11,7 +11,7 @@ from utils import clean_html
|
|||
|
||||
|
||||
class MarkdownConformanceTest_10(TestCase):
|
||||
name = 'Markdown Conformance 1.0'
|
||||
"""Markdown Conformance 1.0"""
|
||||
suite = 'MarkdownTest_1.0'
|
||||
|
||||
def setup(self):
|
||||
|
@ -47,5 +47,5 @@ class MarkdownConformanceTest_10(TestCase):
|
|||
|
||||
|
||||
class MarkdownConformanceTest_103(MarkdownConformanceTest_10):
|
||||
name = 'Markdown Conformance 1.0.3'
|
||||
"""Markdown Conformance 1.0.3"""
|
||||
suite = 'MarkdownTest_1.0.3'
|
||||
|
|
|
@ -6,8 +6,6 @@ from misaka import reduce_dict, extension_map, \
|
|||
|
||||
|
||||
class ReduceDictTest(TestCase):
|
||||
name = 'Reduce Dict'
|
||||
|
||||
def test_from_dict(self):
|
||||
expected = EXT_TABLES | EXT_FENCED_CODE | EXT_FOOTNOTES
|
||||
result = reduce_dict(
|
||||
|
|
|
@ -343,8 +343,6 @@ class TestRendererLowlevel(m.BaseRenderer):
|
|||
|
||||
|
||||
class CustomRendererTest(TestCase):
|
||||
name = 'Custom Renderer'
|
||||
|
||||
def setup(self):
|
||||
render_default = m.Markdown(
|
||||
TestRenderer(), (
|
||||
|
|
|
@ -5,8 +5,6 @@ from misaka import smartypants
|
|||
|
||||
|
||||
class SmartypantsTest(TestCase):
|
||||
name = 'Smartypants'
|
||||
|
||||
def test_apostrophes(self):
|
||||
ok(smartypants('\'s')) == '’s'
|
||||
ok(smartypants('\'t')) == '’t'
|
||||
|
|
Loading…
Reference in New Issue