classification
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
process
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 2020-05-27 21:04 by remi.lapeyre.

Files
File name Uploaded Description Edit
test_functiontest.py remi.lapeyre, 2020-05-27 21:04
Pull Requests
URL Status Linked Edit
PR 20237 open 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=[<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__'
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
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_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.
History
Date User Action Args
2020-05-27 21:04:36remi.lapeyresetfiles: + test_functiontest.py

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