[pytest-dev] The issue with fixtures finalization

Anton P anton7811 at gmail.com
Tue Apr 15 18:47:16 CEST 2014


Hi All,

I found that pytest 2.5.2 doesn't finalize all fixtures. E.g. we have
function and class level fixtures. In case func level fixture raises an
exception, class level fixture finalization won't be called.
SetupState._callfinalizers takes care only about finalizing of all fixtures
with the same level.

I didn't find any way to workaround this without modifying pytest code.

Here is my fix:
1. add self.finalizer_exception=None attribute.
2. Modify _callfinalizers
def _callfinalizers(self, colitem):
    finalizers = self._finalizers.pop(colitem, None)
    while finalizers:
        fin = finalizers.pop()
        try:
            fin()
        except (Exception, KeyboardInterrupt):
            # XXX Only first exception will be seen by user,
            #     ideally all should be reported.
            if self.finalizer_exception is None:
                self.finalizer_exception = sys.exc_info()
    if self.finalizer_exception and not self._finalizers:
        py.builtin._reraise(*self.finalizer_exception)


What do you think about this? Have I missed something? I assume that there
could be another way to resolve the issue.

Thank you in advance!
Anton
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pytest-dev/attachments/20140415/4cc91a23/attachment.html>


More information about the Pytest-dev mailing list