-
-
Notifications
You must be signed in to change notification settings - Fork 82
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
Enhancement: provide a mechanism to use the faker
pytest fixture Faker instance
#441
Comments
I think this can be added without too much trouble. As for your point regarding the scopes, I think it should be fine if we just depend on the @strangemonad is this something you'd be interested to work on? |
@strangemonad I've edited the initial description to end the code block at the right place. |
To provide a bit more clarity on this, I think the fixture that we register with @pytest.fixture
def my_factory_fixture(faker: Faker) -> MyFactory:
MyFactory.__faker__ = faker
return MyFactory |
|
Regarding point one, that's completely fine. I'll try to work on this if I get time, but feel free to let me know if you want to work on it. As for the second point, are you proposing to make Also, I like the idea of setting it on the |
Yes, exactly, I think that's one of the things that still needs hashing out. Overall I think the following feel right:
For scoping / multiple specially configured faker instances, I think there are a few options:
|
I like this idea. I'm just not a 100% sure on how the API would look like while also allowing users to use their own custom faker instances and maintaining backwards compatibility. I think getting the current faker fixture scoped to the function should be possible as well with some approach like you suggested. However, I agree, this would require some investigation and some experimentation to really figure out how to approach this.
I think it should be the other way around. You'd have to opt in to use the session-scoped fixture in order to maintain backwards compatibility.
I think there are two issues with this idea:
|
Summary
Faker comes with a built-in pytest fixture. For both efficiency sake and control over the faker instance customization (e.g. custom faker providers, locales and randomness) it would be nice if there were a way to use that shared instance.
Basic Example
The approach I'm imagining is either as an extra option to
pytest_plugin.registry_factory(... use_fake_fixture=True)
or as a completely separate pytest plugin method.In that case, when the Factory class doesn't define its own
__faker__
instance, the pytestfaker
fixture would be used.e.g.
Drawbacks and Impact
No response
Unresolved questions
__faker__ = Faker()
instance to a sentinel value to avoid creating extraFaker
instances.Note
While we are open for sponsoring on GitHub Sponsors and
OpenCollective, we also utilize Polar.sh to engage in pledge-based sponsorship.
Check out all issues funded or available for funding on our Polar.sh Litestar dashboard
The text was updated successfully, but these errors were encountered: