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:: And run the tests::
python setup.py test # or... python setup.py test # or...
python tests/runner.py python tests/run_tests.py
Usage Usage

View File

@ -20,7 +20,7 @@ class BaseCommand(Command):
class TestCommand(BaseCommand): class TestCommand(BaseCommand):
description = 'run unit tests' description = 'run unit tests'
def run(self): def run(self):
errno = subprocess.call([sys.executable, 'tests/runner.py']) errno = subprocess.call([sys.executable, 'tests/run_tests.py'])
sys.exit(errno) sys.exit(errno)

View File

@ -20,6 +20,17 @@ from collections import namedtuple, defaultdict
Result = namedtuple('Result', ('func', 'name', 'failure')) 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): def _exc_name(exception_class):
if not inspect.isclass(exception_class): if not inspect.isclass(exception_class):
exception_class = exception_class.__class__ exception_class = exception_class.__class__
@ -61,10 +72,12 @@ class AssertionObject(object):
target_length = len(self._target) target_length = len(self._target)
if target_length > other: if target_length > other:
raise AssertionError('Higher than desired length: {!r} > {!r}' raise AssertionError(
'Higher than desired length: {!r} > {!r}'
.format(target_length, other)) .format(target_length, other))
elif 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)) .format(target_length, other))
def diff(self, other): def diff(self, other):
@ -114,7 +127,6 @@ class AssertionObject(object):
.format(name, _exc_name(e), e)) .format(name, _exc_name(e), e))
# A nicer alias.
ok = AssertionObject ok = AssertionObject
@ -127,6 +139,14 @@ class TestCase(object):
if t.startswith('test_'): if t.startswith('test_'):
self._tests.append(self._wrap_test(getattr(self, t))) 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): def add_test(self, func):
self._tests.append(self._wrap_test(func)) self._tests.append(self._wrap_test(func))
@ -136,20 +156,13 @@ class TestCase(object):
try: try:
func() func()
except AssertionError as e: except AssertionError as e: # Expected exception
failure = str(e) failure = str(e)
except Exception as e: except Exception as e: # Unexpected exception
failure = ''.join(traceback.format_exception( failure = ''.join(traceback.format_exception(
*sys.exc_info())).strip() *sys.exc_info())).strip()
doc = '' return Result(func.__name__, _get_doc_line(func) or None, failure)
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 catch_exception return catch_exception
@ -177,8 +190,7 @@ def runner(testcases, setup_func=None, teardown_func=None, config={}):
for testcase in testcases: for testcase in testcases:
tests = testcase(config) tests = testcase(config)
print('>> {}'.format(tests.name if hasattr(tests, 'name') print('>> {}'.format(testcase.name()))
else testcase.__name__))
for result in tests.run(): for result in tests.run():
name = result.name or result.func name = result.name or result.func

View File

@ -45,7 +45,8 @@ def is_test(n):
def get_tests(module): 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=[]): def run_tests(tests, include=[], exclude=[]):

View File

@ -11,7 +11,7 @@ from utils import clean_html
class MarkdownConformanceTest_10(TestCase): class MarkdownConformanceTest_10(TestCase):
name = 'Markdown Conformance 1.0' """Markdown Conformance 1.0"""
suite = 'MarkdownTest_1.0' suite = 'MarkdownTest_1.0'
def setup(self): def setup(self):
@ -47,5 +47,5 @@ class MarkdownConformanceTest_10(TestCase):
class MarkdownConformanceTest_103(MarkdownConformanceTest_10): class MarkdownConformanceTest_103(MarkdownConformanceTest_10):
name = 'Markdown Conformance 1.0.3' """Markdown Conformance 1.0.3"""
suite = 'MarkdownTest_1.0.3' suite = 'MarkdownTest_1.0.3'

View File

@ -6,8 +6,6 @@ from misaka import reduce_dict, extension_map, \
class ReduceDictTest(TestCase): class ReduceDictTest(TestCase):
name = 'Reduce Dict'
def test_from_dict(self): def test_from_dict(self):
expected = EXT_TABLES | EXT_FENCED_CODE | EXT_FOOTNOTES expected = EXT_TABLES | EXT_FENCED_CODE | EXT_FOOTNOTES
result = reduce_dict( result = reduce_dict(

View File

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

View File

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