Skip to content

Nested classes call setup() >1 time #34

Description

@bitprophet

E.g.:

class Outer(Spec):
    def setup(self):
        print "foo"

    def a_test(self):
        assert True

    class inner:
        def another_test(self):
            assert True

Running this file will see setup() run 2x during execution of inner.another_test.

This is problematic when e.g. performing additive state changes within setup, such as calls to mock.patch/mock.patch.start, which add to an internal-on-their-end data structure. Thus, using teardown() to "undo" things done this way in setup() will not suffice as it only undoes the outermost copy of the action.

The problem code is in the __getattr__ wrapper used when decorating the inner classes, which "conveniently" calls setup() on an instance of the outer class. IIRC this was done so that accessing self.parent ran the parent's setup(), otherwise, e.g. self.parent.an_attr_set_during-setup would not exist).

It's unclear why this is leading to 2x calls of the same setup, unless the inner class is also inheriting the setup code somehow (AFAIK this is not the case or we wouldn't even have that silly wrapper.)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions