You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Since upgrading to pytest>=7.0.0, the self parameter between test runs remain the same when retrying a single test function within a test class. When using pytest==6.5.2 (with earlier compatible pytest-rerunfailures versions), it is working as expected, the self parameter refers to a new instance every time the test is run.
To reproduce the issue, you can use the following code:
Running these tests output the following, showing that the same instance is used for self when retrying the test:
SETUP 1
TEST 2
TEARDOWN 3
RSETUP 4
TEST 5
TEARDOWN 6
RSETUP 7
TEST 8
TEARDOWN 9
.SETUP 1
OTHER 2
.TEARDOWN 3
I think this should be considered as a bug, see for example the commit message of pytest-dev/pytest@0dc0360 which states This is definitely broken since it breaks test isolation. for a similar case.
I could work around this issue in a really ugly and fragile way using a pytest hook like this, but I think this should be fixed properly within pytest-rerunfailures:
importpytestimportweakreffromtypingimportAny, Generatorlast_weak_obj_key=pytest.StashKey[weakref.ref[Any]]()
last_weak_instance_key=pytest.StashKey[weakref.ref[Any]]()
@pytest.hookimpl(hookwrapper=True, tryfirst=True)defpytest_runtest_setup(item: Any) ->Generator[None, Any, None]:
last_weak_obj=item.stash.get(last_weak_obj_key, lambda: None)
last_weak_instance=item.stash.get(last_weak_instance_key, lambda: None)
assertitem.objisnotNoneifitem.objislast_weak_obj() or (
item.instanceisnotNoneanditem.instanceislast_weak_instance()
):
# Deleting these two internal attributes trigger new instance creation when accessing item.obj or item.instance the next timedelitem._objdelitem._instanceassertitem.objisnotNoneanditem.objisnotlast_weak_obj()
item.stash[last_weak_obj_key] =weakref.ref(item.obj)
assertitem.instanceisNoneoritem.instanceisnotlast_weak_instance()
item.stash[last_weak_instance_key] =weakref.ref(item.instance)
yield
The text was updated successfully, but these errors were encountered:
Since upgrading to
pytest>=7.0.0
, theself
parameter between test runs remain the same when retrying a single test function within a test class. When usingpytest==6.5.2
(with earlier compatiblepytest-rerunfailures
versions), it is working as expected, theself
parameter refers to a new instance every time the test is run.To reproduce the issue, you can use the following code:
Running these tests output the following, showing that the same instance is used for
self
when retrying the test:I think this should be considered as a bug, see for example the commit message of pytest-dev/pytest@0dc0360 which states
This is definitely broken since it breaks test isolation.
for a similar case.I could work around this issue in a really ugly and fragile way using a pytest hook like this, but I think this should be fixed properly within
pytest-rerunfailures
:The text was updated successfully, but these errors were encountered: