Skip to content

Use fixtures to implement xUnit setup/teardown  #3094

@nicoddemus

Description

@nicoddemus

As discussed in #517.

Implement setup_*/teardown_* using fixtures injected during collection.

During collection, TestCase subclasses with a setup_ and/or teardown_ methods would have this fixture injected in the class:

@pytest.fixture(autouse=True)
def _xunit_setup_teardown_function(self):
    if hasattr(self, 'setup_method'):
        self.setup_method(self)
    yield
    if hasattr(self, 'teardown_method'):
        self.teardown_method(self)

If neither method is present, then this fixture is not injected at all.

The same would be done for setUpClass and setUpModule.

This would solve the problems mentioned in #517 quite nicely.


As discussed, this change might introduce subtle errors so this might be better introduced with an option to revert to the old implementation.

This should no longer be a concern, because we will only handle pytest's methods this way.

EDIT: updated regarding #3094 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    plugin: unittestrelated to the unittest integration builtin plugintype: proposalproposal for a new feature, often to gather opinions or design the API around the new feature

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions