Details added in fixture cleanup are not gathered

Reported by Jonathan Lange on 2011-11-28
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Python Fixtures
Wishlist
Unassigned
testtools
Medium
Unassigned

Bug Description

useFixture does this::
        try:
            fixture.setUp()
        except:
            gather_details(fixture.getDetails(), self.getDetails())
            raise
        else:
            self.addCleanup(fixture.cleanUp)
            self.addCleanup(
                gather_details, fixture.getDetails(), self.getDetails())
            return fixture

(python-fixtures has a useFixture that's very similar).

Note that the details gathering will run *before* the call to cleanup. This means any details added in cleanup will not be included in the gathered details.

Here's a test that could go into testtools's test_fixturesupport::

    def test_useFixture_details_captured_from_cleanups(self):
        # Details added during fixture clean up are gathered.
        class CleanupDetail(fixtures.Fixture):
            def setUp(self):
                fixtures.Fixture.setUp(self)
                self.addDetail('content', content.text_content("foobar"))
                self.addCleanup(
                    self.addDetail, 'cleanup', content.text_content("foobar"))
        fixture = CleanupDetail()
        class SimpleTest(TestCase):
            def test_foo(self):
                self.useFixture(fixture)
        result = ExtendedTestResult()
        SimpleTest('test_foo').run(result)
        self.assertEqual('addSuccess', result._events[-2][0])
        details = result._events[-2][2]
        self.assertEqual(['cleanup', 'content'], sorted(details))
        self.assertEqual('foobar', ''.join(details['cleanup'].iter_text()))

It's a pretty easy fix (just swap the addCleanup calls around), but I'm not sure if that introduce different bugs.

Jonathan Lange (jml) wrote :

Not being able to add details during cleanup removes a valuable channel for providing debugging information.

Changed in python-fixtures:
status: New → Triaged
importance: Undecided → Wishlist
Jonathan Lange (jml) on 2011-12-08
Changed in testtools:
status: New → Triaged
importance: Undecided → Medium
Jonathan Lange (jml) on 2012-02-10
tags: added: details fixtures
Robert Collins (lifeless) wrote :

Details can be registered before their content is known, quite trivially. The issue with gathering details after cleanup is that cleanup tends to free and toss away state. So for most well written fixtures its impossible to get details *after* cleanup has happened.

I think the current code is entirely correct but we may want to document this behaviour, and why.

Jonathan Lange (jml) wrote :

Fair enough. IIRC, the particular context where this came up was debugging fixtures that weren't able to clean up properly. I do think handling fixtures that are *not* well-written is an important use case.

To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers