Skip to content
This repository was archived by the owner on Apr 29, 2024. It is now read-only.

Commit e783bd9

Browse files
committed
refactor test code
1 parent 3699852 commit e783bd9

File tree

5 files changed

+49
-83
lines changed

5 files changed

+49
-83
lines changed

tests/base_tester.py

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,16 @@
99
class BasePytestFixtureChecker(CheckerTestCase):
1010
CHECKER_CLASS = VariablesChecker
1111
MSG_ID = None
12+
MESSAGES = None
1213

13-
def _verify(self, module, msg_count, msg_id=None):
14-
self.walk(module) # run all checkers
15-
messages = self.linter.release_messages()
16-
msg_id = msg_id or self.MSG_ID
17-
18-
matched_count = 0
19-
for message in messages:
20-
print(message)
21-
# only care about ID and count, not the content
22-
if message.msg_id == msg_id:
23-
matched_count += 1
24-
25-
assert matched_count == msg_count
26-
27-
def run_test(self, msg_count, enable_plugin, msg_id=None):
14+
def run_linter(self, enable_plugin, file_path=None):
2815
# pylint: disable=protected-access
29-
file = sys._getframe(1).f_code.co_name.replace('test_', '', 1) + '.py'
30-
full_file_path = os.path.join(
31-
os.getcwd(), 'tests', 'input', self.MSG_ID, file)
32-
with open(full_file_path) as fin:
16+
if file_path is None:
17+
module = sys._getframe(1).f_code.co_name.replace('test_', '', 1)
18+
file_path = os.path.join(
19+
os.getcwd(), 'tests', 'input', self.MSG_ID, module + '.py')
20+
21+
with open(file_path) as fin:
3322
content = fin.read()
3423
module = astroid.parse(content)
3524
module.file = fin.name
@@ -38,11 +27,20 @@ def run_test(self, msg_count, enable_plugin, msg_id=None):
3827
register(None)
3928

4029
try:
41-
self._verify(
42-
module=module,
43-
msg_count=msg_count,
44-
msg_id=msg_id,
45-
)
30+
self.walk(module) # run all checkers
31+
self.MESSAGES = self.linter.release_messages()
4632
finally:
4733
if enable_plugin:
4834
unregister()
35+
36+
def verify_messages(self, msg_count, msg_id=None):
37+
msg_id = msg_id or self.MSG_ID
38+
39+
matched_count = 0
40+
for message in self.MESSAGES:
41+
print(message)
42+
# only care about ID and count, not the content
43+
if message.msg_id == msg_id:
44+
matched_count += 1
45+
46+
assert matched_count == msg_count

tests/test_redefined_outer_name.py

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,20 @@ class TestRedefinedOuterName(BasePytestFixtureChecker):
77

88
@pytest.mark.parametrize('enable_plugin', [True, False])
99
def test_smoke(self, enable_plugin):
10-
self.run_test(
11-
enable_plugin=enable_plugin,
12-
msg_count=0 if enable_plugin else 1,
13-
)
10+
self.run_linter(enable_plugin)
11+
self.verify_messages(0 if enable_plugin else 1)
1412

1513
@pytest.mark.parametrize('enable_plugin', [True, False])
1614
def test_caller_yield_fixture(self, enable_plugin):
17-
self.run_test(
18-
enable_plugin=enable_plugin,
19-
msg_count=0 if enable_plugin else 1,
20-
)
15+
self.run_linter(enable_plugin)
16+
self.verify_messages(0 if enable_plugin else 1)
2117

2218
@pytest.mark.parametrize('enable_plugin', [True, False])
2319
def test_caller_not_a_test_func(self, enable_plugin):
24-
self.run_test(
25-
enable_plugin=enable_plugin,
26-
msg_count=1,
27-
)
20+
self.run_linter(enable_plugin)
21+
self.verify_messages(1)
2822

2923
@pytest.mark.parametrize('enable_plugin', [True, False])
3024
def test_args_and_kwargs(self, enable_plugin):
31-
self.run_test(
32-
enable_plugin=enable_plugin,
33-
msg_count=2,
34-
)
25+
self.run_linter(enable_plugin)
26+
self.verify_messages(2)

tests/test_regression.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,6 @@ class TestRegression(BasePytestFixtureChecker):
99
@pytest.mark.parametrize('enable_plugin', [True, False])
1010
def test_import_twice(self, enable_plugin):
1111
'''catch a coding error when using fixture + if + inline import'''
12-
self.run_test(
13-
enable_plugin=enable_plugin,
14-
msg_count=2,
15-
msg_id='unused-import'
16-
)
17-
18-
self.run_test(
19-
enable_plugin=enable_plugin,
20-
msg_count=1,
21-
msg_id='redefined-outer-name'
22-
)
12+
self.run_linter(enable_plugin)
13+
self.verify_messages(2, msg_id='unused-import')
14+
self.verify_messages(1, msg_id='redefined-outer-name')

tests/test_unused_argument.py

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,20 @@ class TestUnusedArgument(BasePytestFixtureChecker):
77

88
@pytest.mark.parametrize('enable_plugin', [True, False])
99
def test_smoke(self, enable_plugin):
10-
self.run_test(
11-
enable_plugin=enable_plugin,
12-
msg_count=0 if enable_plugin else 2,
13-
)
10+
self.run_linter(enable_plugin)
11+
self.verify_messages(0 if enable_plugin else 2)
1412

1513
@pytest.mark.parametrize('enable_plugin', [True, False])
1614
def test_caller_yield_fixture(self, enable_plugin):
17-
self.run_test(
18-
enable_plugin=enable_plugin,
19-
msg_count=0 if enable_plugin else 1,
20-
)
15+
self.run_linter(enable_plugin)
16+
self.verify_messages(0 if enable_plugin else 1)
2117

2218
@pytest.mark.parametrize('enable_plugin', [True, False])
2319
def test_caller_not_a_test_func(self, enable_plugin):
24-
self.run_test(
25-
enable_plugin=enable_plugin,
26-
msg_count=1,
27-
)
20+
self.run_linter(enable_plugin)
21+
self.verify_messages(1)
2822

2923
@pytest.mark.parametrize('enable_plugin', [True, False])
3024
def test_args_and_kwargs(self, enable_plugin):
31-
self.run_test(
32-
enable_plugin=enable_plugin,
33-
msg_count=2,
34-
)
25+
self.run_linter(enable_plugin)
26+
self.verify_messages(2)

tests/test_unused_import.py

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,24 @@ class TestUnusedImport(BasePytestFixtureChecker):
77

88
@pytest.mark.parametrize('enable_plugin', [True, False])
99
def test_smoke(self, enable_plugin):
10-
self.run_test(
11-
enable_plugin=enable_plugin,
12-
msg_count=0 if enable_plugin else 1,
13-
)
10+
self.run_linter(enable_plugin)
11+
self.verify_messages(0 if enable_plugin else 1)
1412

1513
@pytest.mark.parametrize('enable_plugin', [True, False])
1614
def test_caller_yield_fixture(self, enable_plugin):
17-
self.run_test(
18-
enable_plugin=enable_plugin,
19-
msg_count=0 if enable_plugin else 1,
20-
)
15+
self.run_linter(enable_plugin)
16+
self.verify_messages(0 if enable_plugin else 1)
2117

2218
@pytest.mark.parametrize('enable_plugin', [True, False])
2319
def test_same_name_arg(self, enable_plugin):
2420
'''an unused import (not a fixture) just happened to have the same
2521
name as fixture - should still raise unused-import warning'''
26-
self.run_test(
27-
enable_plugin=enable_plugin,
28-
msg_count=1,
29-
)
22+
self.run_linter(enable_plugin)
23+
self.verify_messages(1)
3024

3125
@pytest.mark.parametrize('enable_plugin', [True, False])
3226
def test_same_name_decorator(self, enable_plugin):
3327
'''an unused import (not a fixture) just happened to have the same
3428
name as fixture - should still raise unused-import warning'''
35-
self.run_test(
36-
enable_plugin=enable_plugin,
37-
msg_count=1,
38-
)
29+
self.run_linter(enable_plugin)
30+
self.verify_messages(1)

0 commit comments

Comments
 (0)