Skip to content

pytest 4.2.0 unittest.TestCase can no longer access parent instance #5390

@westhomas

Description

@westhomas

In pytest==4.1.1 and earlier, unittest.TestCase functions could access their parent class instance. In pytest==4.2.0 the behavior changed.

Was this intended? This causes some problems on my end if so and I'm looking for a work-around going foward.

# test_foo.py
import unittest


class Test:
    def test_method(self):
        pass

def test_function():
    pass


class Broken(unittest.TestCase):
    def test_method(self):
        pass

# conftest.py
def pytest_runtest_makereport(item, call):
    if call.when == 'call':
        print(item.nodeid, 'obj self:', getattr(item.obj, '__self__', None))
$ pip install pytest==4.1.1
$ pytest -q
testing/test_foo.py::Test::test_method obj self: <test_foo.Test object at 0x7f8d1d337518>
.testing/test_foo.py::test_function obj self: None
.testing/test_foo.py::Broken::test_method obj self: test_method (test_foo.Broken)
.                                                                                                                                                                                                                                  [100%]
3 passed in 0.05 seconds
$ pip install pytest==4.2.0
$ pytest -q
testing/test_foo.py::Test::test_method obj self: <test_foo.Test object at 0x7fc6de6aff28>
.testing/test_foo.py::test_function obj self: None
.testing/test_foo.py::Broken::test_method obj self: None .     <======== this is None!
.                                                                                                                                                                                                                                  [100%]
3 passed in 0.07 seconds

Metadata

Metadata

Assignees

Labels

plugin: unittestrelated to the unittest integration builtin plugintype: regressionindicates a problem that was introduced in a release which was working previously

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions