You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
unittest: if FunctionTestCase is imported, the loader loads "tests" from it #84867
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:
assignee=Noneclosed_at=Nonecreated_at=<Date2020-05-19.20:17:05.761>labels= ['3.8', 'type-bug', 'tests', '3.9', '3.10']
title='unittest: if FunctionTestCase is imported, the loader loads "tests" from it'updated_at=<Date2020-05-27.21:04:36.962>user='https://github.com/abetkin'
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?
abetkin commented on May 22, 2020
abetkinmannequin
MannequinAuthor
More actions
I don't think anyone is using FunctionTestCase
remilapeyre commented on May 27, 2020
remilapeyremannequin
Mannequin
More actions
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
E
======================================================================
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/runpy.py", line 193, in _run_module_as_mainreturn _run_code(code, main_globals, None,
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/runpy.py", line 86, in _run_codeexec(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 runTestsself.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 printErrorsself.printErrorList('ERROR', self.errors)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/unittest/runner.py", line 115, in printErrorListself.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 getDescriptionreturn'\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.
Activity
abetkin commentedon May 19, 2020
In [2]: from unittest import FunctionTestCase
In [3]: loader.loadTestsFromTestCase(FunctionTestCase)
Out[3]: <unittest.suite.TestSuite tests=[<unittest.case.FunctionTestCase tec=runTest>]>
In [4]: test = _._tests[0]; test
Out[4]: <unittest.case.FunctionTestCase tec=runTest>
In [5]: test._testFunc
Out[5]: 'runTest'
In [6]: test._testFunc.__name__
*** AttributeError: 'str' object has no attribute '__name__'
remilapeyre commentedon May 21, 2020
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?
abetkin commentedon May 21, 2020
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.
cjerdonek commentedon May 22, 2020
If this is true, then how is anyone able to be using FunctionTestCase in their tests today? Is the feature broken?
abetkin commentedon May 22, 2020
I don't think anyone is using FunctionTestCase
remilapeyre commentedon May 27, 2020
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
E
======================================================================
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.
pythongh-84867: Do not load tests from `TestCase` and `FunctionTestCase`
gh-84867: Do not load tests from TestCase and FunctionTestCase (GH-10…
pythongh-84867: Do not load tests from TestCase and FunctionTestCase (p…
pythongh-84867: Do not load tests from TestCase and FunctionTestCase (p…
[3.11] gh-84867: Do not load tests from TestCase and FunctionTestCase (…
pythongh-84867: Do not load tests from TestCase and FunctionTestCase (p…
[3.12] gh-84867: Do not load tests from TestCase and FunctionTestCase (…