pytest-dev / pytest-dev/pytest

It would be good to have fixtures with dinamically changed / different scopes

Open
#12,673 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic: fixtures type: question
Dominant language
Python
Stars
14.5k
Forks
3.4k
Avg merge
2d 9h
Merged PRs (30d)
35

Description

What's the problem this feature will solve?

I create random users for the tests. I use factories according to https://docs.pytest.org/en/latest/how-to/fixtures.html#factories-as-fixtures

The user creation is quite expensive (in time), so I would like to create only one user for negative test cases, and one for every positive test case when the user state is changed during the test.

My idea was that create user for negative tests with module scope and use function scope for positive ones. But it is not possible. (I duplicated the fixtures with different scopes, but it is not good)

Describe the solution you'd like

I do not know how to solve this

Alternative Solutions

As I wrote this feature request, it got to my mind that there is a class scope, and that can solve this.:
Create a class for negative test cases and one class for each positive, and it solves the issue.

Perhaps documenting this with an example (two class type: one for all negative test cases and several for the positive ones) should be enough / good

BR,
George

Additional context

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with pytest's fixture factories documentation, especially the scope examples and the class-scope behavior described in the issue. Determine whether dynamically selecting fixture scopes is a defined feature request or whether the existing class-based workaround should be documented; done means the expected approach and scope are clear.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.