Test class instance is not recreated when retrying tests
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Aptitud para principiantes
- 42/100
- Tipo de issue
- Error
- Claridad
- Bastante claro
- Estado de actividad
- Estancado
- Stack tecnológico
- python
- Área
- testing-qa, tooling
Línea de trabajo
Comienza ejecutando la prueba flaky proporcionada con pytest 7 y pytest 6.5.2; después, inspecciona la ruta de reintento alrededor del flaky decorator y del hook pytest_runtest_setup mostrado en el issue. Se considera terminado cuando los reintentos usan una instancia nueva de la clase de prueba sin romper la configuración y el desmontaje de los fixtures, y existe una prueba de regresión que cubre el aislamiento de instancias.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
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, 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:
import pytest
from flaky import flaky
class TestExample:
@property
def counter(self):
self.__dict__.setdefault('_counter', 0)
self._counter += 1
return self._counter
@pytest.fixture(autouse=True)
def some_fixture(self):
print('SETUP', self.counter)
yield
print('TEARDOWN', self.counter)
@flaky(max_runs=5)
def test_something(self, param=[]):
print('TEST', self.counter)
param.append(0)
assert len(param) > 2
def test_other(self):
print('OTHER', self.counter)
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
SETUP 4
TEST 5
TEARDOWN 6
SETUP 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 https://github.com/pytest-dev/pytest/commit/0dc036035107b213c9b73bf965cbd7356111b85a 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 flaky:
import pytest
import weakref
from typing import Any, Generator
last_weak_obj_key = pytest.StashKey[weakref.ref[Any]]()
last_weak_instance_key = pytest.StashKey[weakref.ref[Any]]()
@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_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)
assert item.obj is not None
if item.obj is last_weak_obj() or (
item.instance is not None and item.instance is last_weak_instance()
):
# Deleting these two internal attributes trigger new instance creation when accessing item.obj or item.instance the next time
del item._obj
del item._instance
assert item.obj is not None and item.obj is not last_weak_obj()
item.stash[last_weak_obj_key] = weakref.ref(item.obj)
assert item.instance is None or item.instance is not last_weak_instance()
item.stash[last_weak_instance_key] = weakref.ref(item.instance)
yield
As pytest-rerunfailures is also affected, I've opened this issue there as well: https://github.com/pytest-dev/pytest-rerunfailures/issues/268
- Lenguaje dominante
- Python
- Estrellas
- 399
- Forks
- 58
- Métricas de merge de PR
- Sin PR fusionados en 30 d
Preparar el entorno
- Sin Dockerfile ni archivo de Docker Compose
- Sin plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de box/flaky
-
Dificultad 4/5 3-5 días Aptitud para principiantes 45/100
-
Dificultad 3/5 1-2 días Aptitud para principiantes 38/100
-
Dificultad 3/5 1-2 días Aptitud para principiantes 48/100
-
is this project dead?Abierto
Dificultad 5/5 Más de una semana Aptitud para principiantes 15/100
-
Dificultad 4/5 3-5 días Aptitud para principiantes 25/100
Issues similares
-
bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
micropython/micropython#19906 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
FinanceFlash/unvibecode#597 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 65/100
TomAnthony/YourOwnWeigh#21 ·
Los mantenedores suelen responder en 1 día
-
[BUG]: Compliance rules never see Unity Catalog views as viewsPosiblemente ocupada @wagnercsantos la tomó hoy. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
databrickslabs/ontos#1348 ·
Los mantenedores suelen responder en 3 días
-
Deprecated datetime.utcnow() is called on every log line (DeprecationWarning on Python 3.12+)Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
Los mantenedores suelen responder en 2 días