Skip to content

pytest doesn't display ResourceWarnings that unittest does #5676

Closed
@mraspberry

Description

@mraspberry

pytest doesn't seem to be displaying ResourceWarnings that unittest does display.
I'm using pytest 5.0.1 on python 3.6.8 on Ubuntu 18.04.2 LTS running in WSL on Windows 10.

Function being tested that triggers ResourceWarning in unittest but not pytest:

def trigger_resourcewarning(filename):
    return open(filename).read()

pytest test code:

import test_module

def test_module_trigger_resource_warning(tmp_path):
    contents = 'TEST\n'
    filename = tmp_path / 'test.txt'
    filename.write_text(contents)
    assert test_module.trigger_resourcewarning(filename) == contents

unittest test code:

import os
import tempfile
import unittest
import test_module

class TestModuleTriggerResourceWarning(unittest.TestCase):

    def test_module_trigger_resource_warning(self):
        contents = 'TEST\n'
        with tempfile.NamedTemporaryFile(delete=False) as fd:
            filename = fd.name
            fd.write(contents.encode())
        self.addCleanup(os.unlink, filename)
        self.assertEqual(test_module.trigger_resourcewarning(filename), contents)

pytest output:

platform linux -- Python 3.6.8, pytest-5.0.1, py-1.8.0, pluggy-0.12.0
rootdir: /home/matt/projects/pytest-issue
collected 1 item

tests/test_resource_warning_proof.py .                                   [100%]

unittest output:

/home/matt/projects/pytest-issue/test_module/__init__.py:4: ResourceWarning: unclosed file <_io.TextIOWrapper name='/tmp/tmp1qw9u_dn' mode='r' encoding='UTF-8'>
  return open(filename).read()
.
----------------------------------------------------------------------
Ran 1 test in 0.006s

OK

Metadata

Metadata

Assignees

No one assigned

    Labels

    plugin: warningsrelated to the warnings builtin plugintype: bugproblem that needs to be addressed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions