-
-
Notifications
You must be signed in to change notification settings - Fork 30.7k
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
unittest: if FunctionTestCase is imported, the loader loads "tests" from it #84867
Comments
In [2]: from unittest import FunctionTestCase In [3]: loader.loadTestsFromTestCase(FunctionTestCase) In [4]: test = _._tests[0]; test In [5]: test._testFunc In [6]: test._testFunc.__name__ |
Hi Vitalii, can you give more context about why you are writing this code, what you expect it to do and why is there a bug? |
If you make an import in a module with your tests From unittest import FunctionTestCase then you will have 1 extra test in that module (the imported one), moreover, that test will be broken. |
If this is true, then how is anyone able to be using FunctionTestCase in their tests today? Is the feature broken? |
I don't think anyone is using FunctionTestCase |
I checked and FunctionTestCase seems to completely break the loader. The tests for FunctionTestCase in the standard library instantiate the class from inside the method of a TestCase so the loader never see them but even the simple test file I attached completely breaks: ✗ python3 -m unittest Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/runpy.py", line 193, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/runpy.py", line 86, in _run_code
exec(code, run_globals)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/unittest/__main__.py", line 18, in <module>
main(module=None)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/unittest/main.py", line 101, in __init__
self.runTests()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/unittest/main.py", line 271, in runTests
self.result = testRunner.run(self.test)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/unittest/runner.py", line 183, in run
result.printErrors()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/unittest/runner.py", line 109, in printErrors
self.printErrorList('ERROR', self.errors)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/unittest/runner.py", line 115, in printErrorList
self.stream.writeln("%s: %s" % (flavour,self.getDescription(test)))
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/unittest/runner.py", line 47, in getDescription
return '\n'.join((str(test), doc_first_line))
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/unittest/case.py", line 1472, in __str__
self._testFunc.__name__)
AttributeError: 'str' object has no attribute '__name__' I look at plenty of usages of FunctionTestCase on Github and all of them seemed to be false positive, they were copies of the unittest/test/test_functiontestcase.py file The patch in the attached PR is not correct thought, it only fixes one of the loader and all of them suffer from the same issue. |
…ythonGH-100497) (cherry picked from commit 66d1d7e) Co-authored-by: Nikita Sobolev <[email protected]>
…ythonGH-100497) (cherry picked from commit 66d1d7e) Co-authored-by: Nikita Sobolev <[email protected]>
…GH-100497) (GH-109328) (cherry picked from commit 66d1d7e) Co-authored-by: Nikita Sobolev <[email protected]>
…GH-100497) (#109327) gh-84867: Do not load tests from TestCase and FunctionTestCase (GH-100497) (cherry picked from commit 66d1d7e) Co-authored-by: Nikita Sobolev <[email protected]>
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
Linked PRs
TestCase
andFunctionTestCase
#100497The text was updated successfully, but these errors were encountered: