Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pytest faker fixture is not session-based #2055

Closed
jgentil opened this issue Jun 3, 2024 · 5 comments
Closed

pytest faker fixture is not session-based #2055

jgentil opened this issue Jun 3, 2024 · 5 comments
Labels

Comments

@jgentil
Copy link

jgentil commented Jun 3, 2024

  • Faker version: 25.3.0
  • OS: any

The faker documentation states here:

Out of the box, the faker fixture returns a session-scoped Faker instance to be used across all tests in your test suite.

But here it clearly is not defined to the session:

@pytest.fixture()
def faker(request):
"""Fixture that returns a seeded and suitable ``Faker`` instance."""

and there's even a "hidden" session-scoped faker:
@pytest.fixture(scope="session", autouse=True)
def _session_faker(request):
"""Fixture that stores the session level ``Faker`` instance.

I noticed that this functionality diverges from the original pytest-faker.

Steps to reproduce

@pytest.fixture(scope="session")
def current_user(faker):
    data = {
            "id":faker.uuid4(),
            "username":faker.user_name(),
            "first_name":faker.first_name(),
            "last_name":faker.last_name(),
            "email":faker.company_email(),
            "avatar":faker.file_name(category="image", extension="jpg"),
            "created_at":timezone.now()   timedelta(days=faker.random_int()),
            "updated_at":timezone.now()   timedelta(days=faker.random_int()),
    }
    user, _ = User.objects.get_or_create(**data)
ERROR [ 37%]
test setup failed
ScopeMismatch: You tried to access the function scoped fixture faker with a session scoped request object. Requesting fixture stack:
conftest.py:313:  def current_user(faker)
Requested fixture:
../../../Library/Caches/pypoetry/virtualenvs/api-YoAtlwgJ-py3.12/lib/python3.12/site-packages/faker/contrib/pytest/plugin.py:23:  def faker(request)

I need to scope a fixture that users faker to a session and I am finding it impossible to do.

@fcurella
Copy link
Collaborator

fcurella commented Jun 3, 2024

Thank you for the report @jgentil ! Do you mind submitting a Pull Request

@jgentil
Copy link
Author

jgentil commented Jun 3, 2024

Yep I'd be glad to!

Would you prefer it be a separate "feature" that implements a session-based faker, or consider it a bug and fix this specific faker to be session-based? I'm worried about backward-compatibility if this has been used often or has become expected behavior.

@fcurella
Copy link
Collaborator

fcurella commented Jun 3, 2024

I would consider it a bug, but still increment the MAJOR version when we release

Copy link

github-actions bot commented Sep 2, 2024

This issue is stale because it has been open for 30 days with no activity.

@github-actions github-actions bot added the stale label Sep 2, 2024
Copy link

This issue was closed because it has been inactive for 14 days since being marked as stale.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Sep 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants