Run refactoring test using py.test

refactor.collect_file_tests is fixed;  it uses global variable
refactoring_test_dir which is not defined when refactor is used
as a module.
This commit is contained in:
Takafumi Arakaki
2013-03-12 06:46:31 +01:00
parent fb0b8b0fc1
commit a31ba8737a
3 changed files with 22 additions and 7 deletions

View File

@@ -2,6 +2,7 @@ from os.path import join, dirname, abspath
default_base_dir = join(dirname(abspath(__file__)), 'completion')
import run
import refactor
def pytest_addoption(parser):
@@ -38,11 +39,15 @@ def pytest_generate_tests(metafunc):
"""
:type metafunc: _pytest.python.Metafunc
"""
if 'case' in metafunc.fixturenames:
base_dir = metafunc.config.option.base_dir
test_files = dict(map(parse_test_files_option,
metafunc.config.option.test_files))
if 'case' in metafunc.fixturenames:
thirdparty = metafunc.config.option.thirdparty
metafunc.parametrize(
'case',
run.collect_dir_tests(base_dir, test_files, thirdparty))
if 'refactor_case' in metafunc.fixturenames:
metafunc.parametrize(
'refactor_case',
refactor.collect_dir_tests(base_dir, test_files))

View File

@@ -64,7 +64,7 @@ class RefactoringCase(object):
self.name, self.line_nr - 1)
def collect_file_tests(source, f_name, lines_to_execute):
def collect_file_tests(source, path, lines_to_execute):
r = r'^# --- ?([^\n]*)\n((?:(?!\n# \+\+\+).)*)' \
r'\n# \+\+\+((?:(?!\n# ---).)*)'
for match in re.finditer(r, source, re.DOTALL | re.MULTILINE):
@@ -86,7 +86,6 @@ def collect_file_tests(source, f_name, lines_to_execute):
if lines_to_execute and line_nr - 1 not in lines_to_execute:
continue
path = os.path.join(os.path.abspath(refactoring_test_dir), f_name)
yield RefactoringCase(name, source, line_nr, index, path,
new_name, start_line_test, second)
@@ -96,10 +95,10 @@ def collect_dir_tests(base_dir, test_files):
files_to_execute = [a for a in test_files.items() if a[0] in f_name]
lines_to_execute = reduce(lambda x, y: x + y[1], files_to_execute, [])
if f_name.endswith(".py") and (not test_files or files_to_execute):
path = os.path.join(refactoring_test_dir, f_name)
path = os.path.join(base_dir, f_name)
with open(path) as f:
source = f.read()
for case in collect_file_tests(source, f_name, lines_to_execute):
for case in collect_file_tests(source, path, lines_to_execute):
yield case

View File

@@ -101,3 +101,14 @@ def test_integration(case, monkeypatch, pytestconfig):
TEST_USAGES: run_related_name_test,
}
testers[case.test_type](case)
def test_refactor(refactor_case):
"""
Run refactoring test case.
:type refactor_case: :class:`.refactor.RefactoringCase`
"""
refactor_case.run()
result, desired = refactor_case.result, refactor_case.desired
assert result == desired, "Refactoring test %r fails" % refactor_case