This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: unittest: if FunctionTestCase is imported, the loader loads "tests" from it
Type: behavior Stage: patch review
Components: Tests Versions: Python 3.10, Python 3.9, Python 3.8
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: chris.jerdonek, pwtail, remi.lapeyre
Priority: normal Keywords: patch

Created on 2020-05-19 20:17 by pwtail, last changed 2022-04-11 14:59 by admin.

File name Uploaded Description Edit remi.lapeyre, 2020-05-27 21:04
Pull Requests
URL Status Linked Edit
PR 20237 closed pwtail, 2020-05-19 20:26
Messages (6)
msg369386 - (view) Author: Vitalii (pwtail) Date: 2020-05-19 20:17
In [2]: from unittest import FunctionTestCase                                                                                                                                                                                  

In [3]: loader.loadTestsFromTestCase(FunctionTestCase)                                                                                                                                                                              
Out[3]: <unittest.suite.TestSuite tests=[< tec=runTest>]>

In [4]: test = _._tests[0]; test 
Out[4]: < tec=runTest>

In [5]: test._testFunc                                                                                                                                                                                                                
Out[5]: 'runTest'

In [6]: test._testFunc.__name__                                                                                                                                                                                                       
*** AttributeError: 'str' object has no attribute '__name__'
msg369524 - (view) Author: Rémi Lapeyre (remi.lapeyre) * Date: 2020-05-21 13:54
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?
msg369547 - (view) Author: Vitalii (pwtail) Date: 2020-05-21 22:34
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.
msg369571 - (view) Author: Chris Jerdonek (chris.jerdonek) * (Python committer) Date: 2020-05-22 08:55
> 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?
msg369575 - (view) Author: Vitalii (pwtail) Date: 2020-05-22 09:31
I don't think anyone is using FunctionTestCase
msg370135 - (view) Author: Rémi Lapeyre (remi.lapeyre) * Date: 2020-05-27 21:04
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/", 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/", line 86, in _run_code
    exec(code, run_globals)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/unittest/", line 18, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/unittest/", line 101, in __init__
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/unittest/", line 271, in runTests
    self.result =
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/unittest/", line 183, in run
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/unittest/", line 109, in printErrors
    self.printErrorList('ERROR', self.errors)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/unittest/", line 115, in printErrorList"%s: %s" % (flavour,self.getDescription(test)))
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/unittest/", line 47, in getDescription
    return '\n'.join((str(test), doc_first_line))
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/unittest/", line 1472, in __str__
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/ 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.
Date User Action Args
2022-04-11 14:59:31adminsetgithub: 84867
2020-05-27 21:04:36remi.lapeyresetfiles: +

messages: + msg370135
versions: + Python 3.10
2020-05-22 09:31:55pwtailsetmessages: + msg369575
2020-05-22 08:55:48chris.jerdoneksetnosy: + chris.jerdonek
messages: + msg369571
2020-05-21 22:34:31pwtailsetmessages: + msg369547
2020-05-21 13:54:27remi.lapeyresetnosy: + remi.lapeyre
messages: + msg369524
2020-05-19 20:26:27pwtailsetkeywords: + patch
stage: patch review
pull_requests: + pull_request19524
2020-05-19 20:17:05pwtailcreate