forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_test_naming.py
155 lines (131 loc) · 5.11 KB
/
check_test_naming.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
"""
Check that test names start with `test`, and that test classes start with `Test`.
This is meant to be run as a pre-commit hook - to run it manually, you can do:
pre-commit run check-test-naming --all-files
NOTE: if this finds a false positive, you can add the comment `# not a test` to the
class or function definition. Though hopefully that shouldn't be necessary.
"""
from __future__ import annotations
import argparse
import ast
import os
from pathlib import Path
import sys
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from collections.abc import (
Iterator,
Sequence,
)
PRAGMA = "# not a test"
def _find_names(node: ast.Module) -> Iterator[str]:
for _node in ast.walk(node):
if isinstance(_node, ast.Name):
yield _node.id
elif isinstance(_node, ast.Attribute):
yield _node.attr
def _is_fixture(node: ast.expr) -> bool:
if isinstance(node, ast.Call):
node = node.func
return (
isinstance(node, ast.Attribute)
and node.attr == "fixture"
and isinstance(node.value, ast.Name)
and node.value.id == "pytest"
)
def _is_register_dtype(node):
return isinstance(node, ast.Name) and node.id == "register_extension_dtype"
def is_misnamed_test_func(
node: ast.expr | ast.stmt, names: Sequence[str], line: str
) -> bool:
return (
isinstance(node, ast.FunctionDef)
and not node.name.startswith("test")
and names.count(node.name) == 0
and not any(_is_fixture(decorator) for decorator in node.decorator_list)
and PRAGMA not in line
and node.name
not in ("teardown_method", "setup_method", "teardown_class", "setup_class")
)
def is_misnamed_test_class(
node: ast.expr | ast.stmt, names: Sequence[str], line: str
) -> bool:
return (
isinstance(node, ast.ClassDef)
and not node.name.startswith("Test")
and names.count(node.name) == 0
and not any(_is_register_dtype(decorator) for decorator in node.decorator_list)
and PRAGMA not in line
)
def main(content: str, file: str) -> int:
lines = content.splitlines()
tree = ast.parse(content)
names = list(_find_names(tree))
ret = 0
for node in tree.body:
if is_misnamed_test_func(node, names, lines[node.lineno - 1]):
print(
f"{file}:{node.lineno}:{node.col_offset} "
"found test function which does not start with 'test'"
)
ret = 1
elif is_misnamed_test_class(node, names, lines[node.lineno - 1]):
print(
f"{file}:{node.lineno}:{node.col_offset} "
"found test class which does not start with 'Test'"
)
ret = 1
if (
isinstance(node, ast.ClassDef)
and names.count(node.name) == 0
and not any(
_is_register_dtype(decorator) for decorator in node.decorator_list
)
and PRAGMA not in lines[node.lineno - 1]
):
for _node in node.body:
if is_misnamed_test_func(_node, names, lines[_node.lineno - 1]):
# It could be that this function is used somewhere by the
# parent class. For example, there might be a base class
# with
#
# class Foo:
# def foo(self):
# assert 1+1==2
# def test_foo(self):
# self.foo()
#
# and then some subclass overwrites `foo`. So, we check that
# `self.foo` doesn't appear in any of the test classes.
# Note some false negatives might get through, but that's OK.
# This is good enough that has helped identify several examples
# of tests not being run.
assert isinstance(_node, ast.FunctionDef) # help mypy
should_continue = False
for _file in (Path("pandas") / "tests").rglob("*.py"):
with open(os.path.join(_file), encoding="utf-8") as fd:
_content = fd.read()
if f"self.{_node.name}" in _content:
should_continue = True
break
if should_continue:
continue
print(
f"{file}:{_node.lineno}:{_node.col_offset} "
"found test function which does not start with 'test'"
)
ret = 1
return ret
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("paths", nargs="*")
args = parser.parse_args()
ret = 0
for file in args.paths:
filename = os.path.basename(file)
if not (filename.startswith("test") and filename.endswith(".py")):
continue
with open(file, encoding="utf-8") as fd:
content = fd.read()
ret |= main(content, file)
sys.exit(ret)