Only define a testcase name when its more readable.

This commit is contained in:
Frank Smit 2015-08-07 18:02:28 +02:00
parent a23c65e206
commit 602b122c8d
8 changed files with 33 additions and 26 deletions

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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=[]):

View File

@ -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'

View File

@ -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(

View File

@ -343,8 +343,6 @@ class TestRendererLowlevel(m.BaseRenderer):
class CustomRendererTest(TestCase):
name = 'Custom Renderer'
def setup(self):
render_default = m.Markdown(
TestRenderer(), (

View File

@ -5,8 +5,6 @@ from misaka import smartypants
class SmartypantsTest(TestCase):
name = 'Smartypants'
def test_apostrophes(self):
ok(smartypants('\'s')) == '&rsquo;s'
ok(smartypants('\'t')) == '&rsquo;t'