diff --git a/docs/index.rst b/docs/index.rst index c277a7a..ae4e98c 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -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 diff --git a/setup.py b/setup.py index 3ca3fb2..6b0e52e 100644 --- a/setup.py +++ b/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) diff --git a/tests/chibitest.py b/tests/chibitest.py index ce07771..086680b 100644 --- a/tests/chibitest.py +++ b/tests/chibitest.py @@ -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 diff --git a/tests/runner.py b/tests/run_tests.py similarity index 95% rename from tests/runner.py rename to tests/run_tests.py index 089eae0..07522c0 100644 --- a/tests/runner.py +++ b/tests/run_tests.py @@ -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=[]): diff --git a/tests/test_markdown.py b/tests/test_markdown.py index 031b37e..b52fd49 100644 --- a/tests/test_markdown.py +++ b/tests/test_markdown.py @@ -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' diff --git a/tests/test_misc.py b/tests/test_misc.py index 16acf5b..4fa0587 100644 --- a/tests/test_misc.py +++ b/tests/test_misc.py @@ -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( diff --git a/tests/test_renderer.py b/tests/test_renderer.py index 5cce810..0162adf 100644 --- a/tests/test_renderer.py +++ b/tests/test_renderer.py @@ -343,8 +343,6 @@ class TestRendererLowlevel(m.BaseRenderer): class CustomRendererTest(TestCase): - name = 'Custom Renderer' - def setup(self): render_default = m.Markdown( TestRenderer(), ( diff --git a/tests/test_smartypants.py b/tests/test_smartypants.py index f774c91..1cfb216 100644 --- a/tests/test_smartypants.py +++ b/tests/test_smartypants.py @@ -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'