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