Diving into pytest Finalizers
Blog post from Mergify
Rémy Duthu shares the challenges encountered while developing pytest-mergify, a plugin designed to rerun a single pytest test multiple times, particularly focusing on handling fixture teardowns during test reruns. The main issue arises when pytest reuses setup states from previous runs, causing tests with fixtures like temporary databases to fail due to lack of reinitialization. Duthu explores how pytest manages fixture scopes and teardowns, discovering that it keeps track of necessary fixture cleanups using private attributes. The problem is solved by adopting a method used by the pytest-rerunfailures plugin, which employs suspended finalizers to prevent higher-scoped fixtures from being torn down between retries, only allowing function-scoped fixtures to reset. This approach maintains the integrity of the internal setup stack and ensures that the teardown process remains consistent and minimal, allowing pytest to function normally with other plugins. Duthu emphasizes the value of understanding pytest's behavior through its source code, highlighting that this insight is more valuable than the fix itself.