Skip to content

Commit c6279d3

Browse files
committed
Minor tests/base_tester.py improvements
* `IMPACTED_CHECKER_CLASSES` is a `type[BaseChecker]`, not a `BaseChecker` (we are instantiating it) * Replace `sys._getframe` "implementation detail" with a standard `pytest` autouse `fixture` * Make sure our test corpus does not raise any warnings (Deus Ex Machina from the future) Signed-off-by: Stavros Ntentos <[email protected]>
1 parent 9e899ef commit c6279d3

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

tests/base_tester.py

+19-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
from __future__ import annotations
22

3+
import logging
34
import os
4-
import sys
55
from abc import ABC
66
from pathlib import Path
77
from pprint import pprint
88

99
import astroid
10+
import pytest
1011
from pylint.checkers import BaseChecker
1112
from pylint.testutils import MessageTest, UnittestLinter
1213
from pylint.utils import ASTWalker
@@ -28,28 +29,41 @@ class BasePytestTester(ABC):
2829
MSG_ID: str
2930
msgs: list[MessageTest] = []
3031

32+
# Set by ``test_name_fixture``
33+
test_name: str = ""
34+
35+
@pytest.fixture(autouse=True)
36+
def test_name_fixture(self, request):
37+
self.test_name = request.node.originalname.replace("test_", "", 1)
38+
3139
def __init_subclass__(cls, **kwargs):
3240
super().__init_subclass__(**kwargs)
3341
if not hasattr(cls, "MSG_ID") or not isinstance(cls.MSG_ID, str) or not cls.MSG_ID:
3442
raise TypeError("Subclasses must define a non-empty MSG_ID of type str")
3543

3644
enable_plugin = True
3745

46+
@pytest.fixture(autouse=True)
47+
def with_no_warnings(self, caplog): # pylint: disable=no-self-use
48+
with caplog.at_level(logging.WARNING):
49+
yield
50+
51+
records = caplog.get_records("call")
52+
assert not records, records
53+
3854
def run_linter(self, enable_plugin):
3955
self.enable_plugin = enable_plugin
4056

41-
# pylint: disable-next=protected-access
42-
target_test_file = sys._getframe(1).f_code.co_name.replace("test_", "", 1)
4357
file_path = os.path.join(
4458
get_test_root_path(),
4559
"input",
4660
self.MSG_ID,
47-
target_test_file + ".py",
61+
self.test_name + ".py",
4862
)
4963

5064
with open(file_path) as fin:
5165
content = fin.read()
52-
module = astroid.parse(content, module_name=target_test_file)
66+
module = astroid.parse(content, module_name=self.test_name)
5367
module.file = fin.name
5468

5569
self.walk(module) # run all checkers

0 commit comments

Comments
 (0)