Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
westhomas opened this issue Jun 4, 2019 · 8 comments

Comments

Projects
None yet
3 participants
@westhomas
Copy link
Contributor

commented Jun 4, 2019

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
@blueyed

This comment has been minimized.

Copy link
Contributor

commented Jun 4, 2019

Thanks for the report!
This sounds like it might also cause the issues seen in pytest-django, which was handling Django's unittest by itself before pytest 4.2, and (had to) use pytest's mechanism in 4.2+ - causing issues there then.

@nicoddemus

This comment has been minimized.

Copy link
Member

commented Jun 4, 2019

Hmm it seems this was introduced with the use of xunit fixtures for setup/teardown in unittest.TestCase subclasses.

item.parent.obj gives the class (not the instance), perhaps that's enough for your needs @westhomas? 🤔

@blueyed

This comment has been minimized.

Copy link
Contributor

commented Jun 4, 2019

Yeah, related to pass_self=False in

cls, "setUpClass", "tearDownClass", scope="class", pass_self=False
.

@nicoddemus

This comment has been minimized.

Copy link
Member

commented Jun 4, 2019

Yeah, related to pass_self=False

Are you sure or are you guessing? 🤔

They don't seem related because the example doesn't have either a setUp or setUpClass, and also if I comment out the code that injects the fixtures the behavior remains unchanged.

@nicoddemus nicoddemus self-assigned this Jun 4, 2019

@nicoddemus

This comment has been minimized.

Copy link
Member

commented Jun 4, 2019

Found the issue! Working on a fix. 😁

@nicoddemus

This comment has been minimized.

Copy link
Member

commented Jun 4, 2019

PR open: #5393

@blueyed

This comment has been minimized.

Copy link
Contributor

commented Jun 4, 2019

Are you sure or are you guessing? 

I've seen an error when changing it to True, which is the case with your PR still, and likely should just not be used like this then.. :D - i.e. I was guessing.. :)

nicoddemus added a commit to nicoddemus/pytest that referenced this issue Jun 4, 2019

nicoddemus added a commit to nicoddemus/pytest that referenced this issue Jun 4, 2019

@nicoddemus

This comment has been minimized.

Copy link
Member

commented Jun 4, 2019

I was guessing.. :)

Fair enough. 😁

I was asking while I was still investigating, to know if I should pursue that course. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.