diff options
author | Derek Gustafson <degustaf@gmail.com> | 2016-12-08 20:34:58 +0000 |
---|---|---|
committer | Derek Gustafson <degustaf@gmail.com> | 2016-12-08 20:34:58 +0000 |
commit | 3f7b2c626dcb991377a1a2b3cbc681edd531cb80 (patch) | |
tree | 1c514391dceede1e117f3a6e9a1d2011704bf5b3 | |
parent | 197555aecfb71a053670b15466c533d988d8f90d (diff) | |
download | pylint-git-3f7b2c626dcb991377a1a2b3cbc681edd531cb80.tar.gz |
Parametrized tests
-rw-r--r-- | pylint/test/extensions/test_check_docs_utils.py | 99 | ||||
-rw-r--r-- | pylint/test/unittest_checker_similar.py | 71 | ||||
-rw-r--r-- | pylint/test/unittest_checkers_utils.py | 52 | ||||
-rw-r--r-- | pylint/test/unittest_lint.py | 72 |
4 files changed, 125 insertions, 169 deletions
diff --git a/pylint/test/extensions/test_check_docs_utils.py b/pylint/test/extensions/test_check_docs_utils.py index ac0b48ea2..a5cf48cea 100644 --- a/pylint/test/extensions/test_check_docs_utils.py +++ b/pylint/test/extensions/test_check_docs_utils.py @@ -9,69 +9,54 @@ in particular the parameter documentation checker `DocstringChecker` """ from __future__ import division, print_function, absolute_import +import pytest + import astroid import pylint.extensions._check_docs_utils as utils -def test_space_indentation(): +@pytest.mark.parametrize("string,count", [ + ('abc', 0), + ('', 0), + (' abc', 2), + ('\n abc', 0), + (' \n abc', 3), +]) +def test_space_indentation(string, count): """Test for pylint_plugin.ParamDocChecker""" - assert utils.space_indentation('abc') == 0 - assert utils.space_indentation('') == 0 - assert utils.space_indentation(' abc') == 2 - assert utils.space_indentation('\n abc') == 0 - assert utils.space_indentation(' \n abc') == 3 + assert utils.space_indentation(string) == count -def test_exception_class(): - raise_node = astroid.extract_node(''' +@pytest.mark.parametrize("raise_node,expected", [ + (astroid.extract_node(''' def my_func(): raise NotImplementedError #@ - ''') - found = utils.possible_exc_types(raise_node) - expected = set(["NotImplementedError"]) - assert found == expected - + '''), set(["NotImplementedError"])), -def test_exception_instance(): - raise_node = astroid.extract_node(''' + (astroid.extract_node(''' def my_func(): raise NotImplementedError("Not implemented!") #@ - ''') - found = utils.possible_exc_types(raise_node) - expected = set(["NotImplementedError"]) - assert found == expected - + '''), set(["NotImplementedError"])), -def test_rethrow(): - raise_node = astroid.extract_node(''' + (astroid.extract_node(''' def my_func(): try: fake_func() except RuntimeError: raise #@ - ''') - found = utils.possible_exc_types(raise_node) - expected = set(["RuntimeError"]) - assert found == expected - + '''), set(["RuntimeError"])), -def test_nested_in_if_rethrow(): - raise_node = astroid.extract_node(''' + (astroid.extract_node(''' def my_func(): try: fake_func() except RuntimeError: if another_func(): raise #@ - ''') - found = utils.possible_exc_types(raise_node) - expected = set(["RuntimeError"]) - assert found == expected - + '''), set(["RuntimeError"])), -def test_nested_in_try(): - raise_node = astroid.extract_node(''' + (astroid.extract_node(''' def my_func(): try: fake_func() @@ -81,14 +66,9 @@ def test_nested_in_try(): raise #@ except NameError: pass - ''') - found = utils.possible_exc_types(raise_node) - expected = set(["RuntimeError"]) - assert found == expected + '''), set(["RuntimeError"])), - -def test_nested_in_try_except(): - raise_node = astroid.extract_node(''' + (astroid.extract_node(''' def my_func(): try: fake_func() @@ -97,47 +77,34 @@ def test_nested_in_try_except(): another_func() except NameError: raise #@ - ''') - found = utils.possible_exc_types(raise_node) - expected = set(["NameError"]) - assert found == expected - + '''), set(["NameError"])), -def test_no_rethrow_types(): - raise_node = astroid.extract_node(''' + (astroid.extract_node(''' def my_func(): try: fake_func() except: raise #@ - ''') - found = utils.possible_exc_types(raise_node) - expected = set() - assert found == expected - + '''), set()), -def test_multiple_rethrow_types(): - raise_node = astroid.extract_node(''' + (astroid.extract_node(''' def my_func(): try: fake_func() except (RuntimeError, ValueError): raise #@ - ''') - found = utils.possible_exc_types(raise_node) - expected = set(["RuntimeError", "ValueError"]) - assert found == expected - + '''), set(["RuntimeError", "ValueError"])), -def test_ignores_uninferable_type(): - raise_node = astroid.extract_node(''' + (astroid.extract_node(''' import not_a_module def my_func(): try: fake_func() except not_a_module.Error: raise #@ - ''') + '''), set()), + +]) +def test_exception(raise_node, expected): found = utils.possible_exc_types(raise_node) - expected = set() assert found == expected diff --git a/pylint/test/unittest_checker_similar.py b/pylint/test/unittest_checker_similar.py index b9627371f..5d536bd2f 100644 --- a/pylint/test/unittest_checker_similar.py +++ b/pylint/test/unittest_checker_similar.py @@ -15,17 +15,14 @@ from pylint.checkers import similar SIMILAR1 = join(dirname(abspath(__file__)), 'input', 'similar1') SIMILAR2 = join(dirname(abspath(__file__)), 'input', 'similar2') + def test_ignore_comments(): sys.stdout = six.StringIO() - try: + with pytest.raises(SystemExit) as ex: similar.Run(['--ignore-comments', SIMILAR1, SIMILAR2]) - except SystemExit as ex: - assert ex.code == 0 - output = sys.stdout.getvalue() - else: - pytest.fail('not system exit') - finally: - sys.stdout = sys.__stdout__ + assert ex.value.code == 0 + output = sys.stdout.getvalue() + sys.stdout = sys.__stdout__ assert output.strip() == (""" 10 similar lines in 2 files ==%s:0 @@ -46,15 +43,11 @@ TOTAL lines=44 duplicates=10 percent=22.73 def test_ignore_docsrings(): sys.stdout = six.StringIO() - try: + with pytest.raises(SystemExit) as ex: similar.Run(['--ignore-docstrings', SIMILAR1, SIMILAR2]) - except SystemExit as ex: - assert ex.code == 0 - output = sys.stdout.getvalue() - else: - pytest.fail('not system exit') - finally: - sys.stdout = sys.__stdout__ + assert ex.value.code == 0 + output = sys.stdout.getvalue() + sys.stdout = sys.__stdout__ assert output.strip() == (""" 8 similar lines in 2 files ==%s:6 @@ -82,15 +75,11 @@ TOTAL lines=44 duplicates=13 percent=29.55 def test_ignore_imports(): sys.stdout = six.StringIO() - try: + with pytest.raises(SystemExit) as ex: similar.Run(['--ignore-imports', SIMILAR1, SIMILAR2]) - except SystemExit as ex: - assert ex.code == 0 - output = sys.stdout.getvalue() - else: - pytest.fail('not system exit') - finally: - sys.stdout = sys.__stdout__ + assert ex.value.code == 0 + output = sys.stdout.getvalue() + sys.stdout = sys.__stdout__ assert output.strip() == """ TOTAL lines=44 duplicates=0 percent=0.00 """.strip() @@ -98,15 +87,11 @@ TOTAL lines=44 duplicates=0 percent=0.00 def test_ignore_nothing(): sys.stdout = six.StringIO() - try: + with pytest.raises(SystemExit) as ex: similar.Run([SIMILAR1, SIMILAR2]) - except SystemExit as ex: - assert ex.code == 0 - output = sys.stdout.getvalue() - else: - pytest.fail('not system exit') - finally: - sys.stdout = sys.__stdout__ + assert ex.value.code == 0 + output = sys.stdout.getvalue() + sys.stdout = sys.__stdout__ assert output.strip() == (""" 5 similar lines in 2 files ==%s:0 @@ -119,24 +104,18 @@ def test_ignore_nothing(): TOTAL lines=44 duplicates=5 percent=11.36 """ % (SIMILAR1, SIMILAR2)).strip() + def test_help(): sys.stdout = six.StringIO() - try: + with pytest.raises(SystemExit) as ex: similar.Run(['--help']) - except SystemExit as ex: - assert ex.code == 0 - else: - pytest.fail('not system exit') - finally: - sys.stdout = sys.__stdout__ + assert ex.value.code == 0 + sys.stdout = sys.__stdout__ + def test_no_args(): sys.stdout = six.StringIO() - try: + with pytest.raises(SystemExit) as ex: similar.Run([]) - except SystemExit as ex: - assert ex.code == 1 - else: - pytest.fail('not system exit') - finally: - sys.stdout = sys.__stdout__ + assert ex.value.code == 1 + sys.stdout = sys.__stdout__ diff --git a/pylint/test/unittest_checkers_utils.py b/pylint/test/unittest_checkers_utils.py index 996f5f2e0..48bf872fc 100644 --- a/pylint/test/unittest_checkers_utils.py +++ b/pylint/test/unittest_checkers_utils.py @@ -11,37 +11,52 @@ import astroid from pylint.checkers import utils import pytest -def test_is_builtin(): - assert utils.is_builtin('min') is True - assert utils.is_builtin('__builtins__') is True - assert utils.is_builtin('__path__') is False - assert utils.is_builtin('__file__') is False - assert utils.is_builtin('whatever') is False - assert utils.is_builtin('mybuiltin') is False -def testGetArgumentFromCall(): - node = astroid.extract_node('foo(bar=3)') - assert utils.get_argument_from_call(node, keyword='bar') is not None - with pytest.raises(utils.NoSuchArgumentError): - node = astroid.extract_node('foo(3)') - utils.get_argument_from_call(node, keyword='bar') +@pytest.mark.parametrize("name,expected", [ + ('min', True), + ('__builtins__', True), + ('__path__', False), + ('__file__', False), + ('whatever', False), + ('mybuiltin', False), +]) +def test_is_builtin(name, expected): + assert utils.is_builtin(name) == expected + + +@pytest.mark.parametrize("fn,kw", [ + ('foo(3)', {'keyword': 'bar'}), + ('foo(one=a, two=b, three=c)', {'position': 1}), +]) +def testGetArgumentFromCallError(fn, kw): with pytest.raises(utils.NoSuchArgumentError): - node = astroid.extract_node('foo(one=a, two=b, three=c)') - utils.get_argument_from_call(node, position=1) - node = astroid.extract_node('foo(a, b, c)') - assert utils.get_argument_from_call(node, position=1) is not None + node = astroid.extract_node(fn) + utils.get_argument_from_call(node, **kw) + + +@pytest.mark.parametrize("fn,kw", [ + ('foo(bar=3)', {'keyword': 'bar'}), + ('foo(a, b, c)', {'position': 1}), +]) +def testGetArgumentFromCallExists(fn, kw): + node = astroid.extract_node(fn) + assert utils.get_argument_from_call(node, **kw) is not None + + +def testGetArgumentFromCall(): node = astroid.extract_node('foo(a, not_this_one=1, this_one=2)') arg = utils.get_argument_from_call(node, position=2, keyword='this_one') assert 2 == arg.value + node = astroid.extract_node('foo(a)') with pytest.raises(utils.NoSuchArgumentError): utils.get_argument_from_call(node, position=1) with pytest.raises(ValueError): utils.get_argument_from_call(node, None, None) - name = utils.get_argument_from_call(node, position=0) assert name.name == 'a' + def test_error_of_type(): nodes = astroid.extract_node(""" try: pass @@ -59,6 +74,7 @@ def test_error_of_type(): assert utils.error_of_type(nodes[1], Exception) assert not utils.error_of_type(nodes[2], ImportError) + def test_node_ignores_exception(): nodes = astroid.extract_node(""" try: diff --git a/pylint/test/unittest_lint.py b/pylint/test/unittest_lint.py index 91c8e1955..d687fcf37 100644 --- a/pylint/test/unittest_lint.py +++ b/pylint/test/unittest_lint.py @@ -146,46 +146,47 @@ def test_no_args(fake_path): assert sys.path == fake_path -def test_one_arg(fake_path): +@pytest.mark.parametrize("case", [ + ['a/b/'], + ['a/b'], + ['a/b/__init__.py'], + ['a/'], + ['a'], +]) +def test_one_arg(fake_path, case): with tempdir() as chroot: create_files(['a/b/__init__.py']) expected = [join(chroot, 'a')] + ["."] + fake_path - cases = ( - ['a/b/'], - ['a/b'], - ['a/b/__init__.py'], - ['a/'], - ['a'], - ) - assert sys.path == fake_path - for case in cases: - with lint.fix_import_path(case): - assert sys.path == expected - assert sys.path == fake_path + with lint.fix_import_path(case): + assert sys.path == expected + assert sys.path == fake_path -def test_two_similar_args(fake_path): +@pytest.mark.parametrize("case", [ + ['a/b', 'a/c'], + ['a/c/', 'a/b/'], + ['a/b/__init__.py', 'a/c/__init__.py'], + ['a', 'a/c/__init__.py'], +]) +def test_two_similar_args(fake_path, case): with tempdir() as chroot: create_files(['a/b/__init__.py', 'a/c/__init__.py']) expected = [join(chroot, 'a')] + ["."] + fake_path - cases = ( - ['a/b', 'a/c'], - ['a/c/', 'a/b/'], - ['a/b/__init__.py', 'a/c/__init__.py'], - ['a', 'a/c/__init__.py'], - ) - assert sys.path == fake_path - for case in cases: - with lint.fix_import_path(case): - assert sys.path == expected - assert sys.path == fake_path + with lint.fix_import_path(case): + assert sys.path == expected + assert sys.path == fake_path -def test_more_args(fake_path): +@pytest.mark.parametrize("case", [ + ['a/b/c/__init__.py', 'a/d/__init__.py', 'a/e/f.py'], + ['a/b/c', 'a', 'a/e'], + ['a/b/c', 'a', 'a/b/c', 'a/e', 'a'], +]) +def test_more_args(fake_path, case): with tempdir() as chroot: create_files(['a/b/c/__init__.py', 'a/d/__init__.py', 'a/e/f.py']) expected = [ @@ -193,17 +194,10 @@ def test_more_args(fake_path): for suffix in [sep.join(('a', 'b')), 'a', sep.join(('a', 'e'))] ] + ["."] + fake_path - cases = ( - ['a/b/c/__init__.py', 'a/d/__init__.py', 'a/e/f.py'], - ['a/b/c', 'a', 'a/e'], - ['a/b/c', 'a', 'a/b/c', 'a/e', 'a'], - ) - assert sys.path == fake_path - for case in cases: - with lint.fix_import_path(case): - assert sys.path == expected - assert sys.path == fake_path + with lint.fix_import_path(case): + assert sys.path == expected + assert sys.path == fake_path @pytest.fixture @@ -384,11 +378,11 @@ def test_enable_by_symbol(init_linter): def test_enable_report(linter): - assert linter.report_is_enabled('RP0001') is True + assert linter.report_is_enabled('RP0001') linter.disable('RP0001') - assert linter.report_is_enabled('RP0001') is False + assert not linter.report_is_enabled('RP0001') linter.enable('RP0001') - assert linter.report_is_enabled('RP0001') is True + assert linter.report_is_enabled('RP0001') def test_report_output_format_aliased(linter): |