-
Notifications
You must be signed in to change notification settings - Fork 17.5k
Add pre-commit hook to ban usage of unittest.TestCase in unit tests #30775
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
ce561b6
15fd059
9b8daad
e30601d
d7bf634
081207b
661a5d9
876f8f6
4847fd3
da1bc0f
7b26da8
0f62dfb
abf24ca
adf80b9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| #!/usr/bin/env python | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| from __future__ import annotations | ||
|
|
||
| import ast | ||
| import pathlib | ||
| import sys | ||
|
|
||
|
|
||
| def check_test_file(file: str) -> int: | ||
| node = ast.parse(pathlib.Path(file).read_text("utf-8"), file) | ||
|
|
||
| found = 0 | ||
| classes = [c for c in node.body if isinstance(c, ast.ClassDef)] | ||
| for c in classes: | ||
| # Some classes are returned as an ast.Attribute, some as an ast.Name object. Not quite sure why | ||
| parent_classes = [base.attr for base in c.bases if isinstance(base, ast.Attribute)] | ||
| parent_classes.extend([base.id for base in c.bases if isinstance(base, ast.Name)]) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI. In general, this is not a complete solution to find out if your class derives from a TestCase or not. However possibly it is good-enough. The cases that are not handled (potentially) with this code:
HOWEVER. I think it's good-enough :) :
Unless of course you would like to dig deeper and make it "perfect" :)
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cc: @uranusjr -> you have more AST experience so maybe you can have some educational comments here as well.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There’s really no reason at all to subclass from anything when you use Pytest anyway, so I think it’s good enough to just assume any
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah. Happy to merge once static checks fixed. |
||
|
|
||
| if "TestCase" in parent_classes: | ||
| found += 1 | ||
| print(f"The class {c.name} inherits from TestCase, please use pytest instead") | ||
|
|
||
| return found | ||
|
|
||
|
|
||
| def main(*args: str) -> int: | ||
| return sum([check_test_file(file) for file in args[1:]]) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| sys.exit(main(*sys.argv)) | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Name is when you use class directly imported:
The Attr is when you use "module.name" expression - because then from the Python syntax point of view you are reading an attribute of a Name defined by import.
Then what you derive from is effectivally an attribute of loaded 'plugins_manager' Name: