Skip to content

Rescope a fixture #3377

Closed
Closed

Description

@hannahdiels

I'd like to change the scope of a fixture within a class, e.g.

class TestIndexPage:

  @pytest.fixture
  def response(self, django_app):
    return django_app.get(...)

  def test_valid_html(self, response):
    assert_is_valid_html(response.text)

  @pytest.mark.parametrize('href', logo_hrefs)
  def test_logo_shown(self, response, href):
    assert response.html.body.find('a', href=href)
    ...

gets the response again for each test function, but this is more isolation than I need as my tests don't modify the response. I'd like response to be class scoped to improve performance, but django_app is function scoped. The only way of changing the scope that I can think of is copy pasting the response fixture, and recursively all of its dependencies to change the scope:

@fixture(scope='class')
def response_class(self, django_app_class):
  ...

@fixture(scope='class')
def django_app_class(self, db_class):
  ...

@fixture(scope='class')
def db_class(self, more_stuff_class):
  ...

...

This also assumes the function scoped django_app is not set up in case it would interfere; it's not needed by these tests, but it is used by other tests outside of this class. The alternative is to simply merge all tests into 1, but I'd rather avoid that.

Is there perhaps some juicy feature I'm overlooking?

Activity

  1. pytestbot commented on Apr 9, 2018

    @pytestbot
    Contributor

    GitMate.io thinks possibly related issues are #538 (Fixture scope documentation), #2246 (Question regarding fixture visibility), #1588 (Proposal to expand fixtures), #1461 (Suggestion: merge .fixture and .yield_fixture), and #1681 (invocation-scoped fixtures).

  2. nicoddemus commented on Apr 9, 2018

    @nicoddemus
    Member

    Unfortunately I don't think there's any easy solution to this; if your django application does not hold state, perhaps you can change it into a session-scoped fixture instead? You probably only need to actually cleanup/rollback the database before each test, not the django_app object. @pytest-dev/pytest-django-developers might have other thoughts/suggestions.

  3. added
    type: questiongeneral question, might be closed after 2 weeks of inactivity
    on Apr 9, 2018
  4. hannahdiels commented on Apr 10, 2018

    @hannahdiels
    Author

    Thanks, that answers my question. For the record, I've mixed things up a bit when simplifying my example; django_app does not depend on db, but my response fixture depends on user which depends on db etc. I could try a session scoped django app since it has no dependencies.

  5. nicoddemus commented on Apr 10, 2018

    @nicoddemus
    Member

    @timdiels thanks for the follow up. Can we close this?

  6. hannahdiels commented on Apr 10, 2018

    @hannahdiels
    Author

    Yes

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

    type: questiongeneral question, might be closed after 2 weeks of inactivity

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions