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.

classification
Title: Unittest displays ResourceWarning warnings when running tests, it probably should not
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.3, Python 3.4
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: orsenthil Nosy List: Bernt.Røskar.Brenna, ezio.melotti, orsenthil, python-dev, r.david.murray
Priority: normal Keywords:

Created on 2014-02-06 12:13 by Bernt.Røskar.Brenna, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (5)
msg210377 - (view) Author: Bernt Røskar Brenna (Bernt.Røskar.Brenna) * Date: 2014-02-06 12:13
Given the following file test_reswarn.py:

====
import unittest


class TestResourceWarning(unittest.TestCase):
    def test_it(self):
        self.assertIn("TestResourceWarning", open(__file__).read())
====

Running the test:

====
$ python -m unittest test_reswarn.py
./test_reswarn.py:6: ResourceWarning: unclosed file <_io.TextIOWrapper name='./test_reswarn.py' mode='r' encoding='UTF-8'>
  self.assertIn("TestResourceWarning", open(__file__).read())
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
====

When running the unit test using unittest, ResourceWarning warnings are displayed. In my experience using open("filename") without explicitly closing the file is pretty common and would usually not be an error, since the file will be closed when the reference goes out of scope (or am I wrong?).

Therefore I believe that unittest should not emit ResourceWarning warnings.
msg210383 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-02-06 14:20
This is intentional behavior.  In the general case, resource warnings *are* bugs, since not all Python implementations do automatic cleanup.  This behavior should be controllable (I'm not even sure they *can* be turned on if python is not compiled in debug mode).

I don't see resource warnings mentioned in the TextTestRunner docs that mention the other warning controls.  It also seems like perhaps there should be a way to control warnings from the unittest command line.  So I'd say there are some improvements that can be made here, but the basic behavior is working as intended.
msg210386 - (view) Author: Bernt Røskar Brenna (Bernt.Røskar.Brenna) * Date: 2014-02-06 14:30
OK I see.

I'll just use:

python -W ignore:ResourceWarning -m unittest
msg210695 - (view) Author: Senthil Kumaran (orsenthil) * (Python committer) Date: 2014-02-08 22:26
I think, it is simply okay to mention about ResourceWarning being displayed. I spent something thinking and going into mechanics of how it is displayed may be an overkill for that line in unittest.rst. I'll make the change and if you see any further information can be added, please comment on it.
msg210696 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-02-08 22:28
New changeset a8a6dc7f478b by Senthil Kumaran in branch '3.3':
Include the mention of ResourceWarning being displayed by default by the test runner.
http://hg.python.org/cpython/rev/a8a6dc7f478b

New changeset 7fc1e8095fb8 by Senthil Kumaran in branch 'default':
merge from 3.3
http://hg.python.org/cpython/rev/7fc1e8095fb8
History
Date User Action Args
2022-04-11 14:57:58adminsetgithub: 64728
2014-02-08 22:33:27orsenthilsetstatus: open -> closed
assignee: orsenthil
resolution: fixed
stage: resolved
2014-02-08 22:28:52python-devsetnosy: + python-dev
messages: + msg210696
2014-02-08 22:26:03orsenthilsetstatus: pending -> open
versions: + Python 3.4
nosy: + orsenthil

messages: + msg210695
2014-02-07 19:07:44serhiy.storchakasetstatus: open -> pending
2014-02-06 14:30:31Bernt.Røskar.Brennasetmessages: + msg210386
2014-02-06 14:21:08r.david.murraysetnosy: + ezio.melotti
2014-02-06 14:20:32r.david.murraysetnosy: + r.david.murray
messages: + msg210383
2014-02-06 12:13:42Bernt.Røskar.Brennacreate