-
Notifications
You must be signed in to change notification settings - Fork 400
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
Change default faker locale in factory_boy #407
Comments
Indeed: with factory.Faker.override_default_locale('es_ES'):
e1 = ExampleFactory() Or:
The idea is that changing the default locale is mostly used for a few specific tests while the whole suite doesn't care; hence, having a decorator/context manager ensures that the default locale is restored once those tests have run. However, this is not very clear in the docs; I think we should improve them. |
Indeed I think allowing to set a global language is a must, thanks :) By the way, global configuration for additional provider does work. |
I agree with @HACKTIVISTA that an easy (and preferably randomizable) way to change the locale would be a major advantage. Right now I am considering using vanilla faker via a custom If thats something you would be interested in getting a PR please let me know. |
This is something I'd also find useful. I also think it might be useful to be able to attach an existing faker generator, so I filed a ticket: #554 |
Long story short, the `faker` lib uses the en_us default locale, hence phone numbers longer than french ones, which was completely breaking the tests. There is no easy way to set factory_boy's embedded version of faker, so it's just easier to update the fields lengths. See FactoryBoy/factory_boy#407
See workaround here. |
I was about to create a new issue. But found this one. Even with some feedback from me :) I looked into it once again. There are basically 2 ways. As one might find out, the default locale is taken from Another option is to do: factory.Faker._DEFAULT_LOCALE = '...' after importing @rbarrois Do you think the second way can be considered official (except for underscore)? Or do you have any better suggestions? UPD But considering that Django project may contain several applications. It doesn't seem right to change the default locale in just one of the them. Some centralized way would probably be preferable. |
So, let's look at the different situations where one might want to change the default locale:
For the last two cases, we'll have to look into the program setup:
For Django, I believe that the recommended mode is to:
# settings.py
TEST_RUNNER = 'myproject.testing.MyTestRunner'
# myproject/testing.py
import factory
from django.conf import settings
from django.util import translation
import django.test.runner
class MyTestRunner(django.test.runner.DiscoverRunner):
def run_tests(self, test_labels, extra_tests=None, **kwargs):
with factory.Faker.override_default_locale(translation.to_locale(settings.LANGUAGE_CODE)):
return super().run_tests(test_labels, extra_tests=extra_tests, **kwargs) |
@rbarrois Oh, it appears I've got my case covered. Do you think we can document it? Supposedly by adding a common recipe? |
For pytest django I just put it in conftest.py since that loads before any tests start: import faker.config
faker.config.DEFAULT_LOCALE = "en_NZ" Works a charm, if you check the Faker class, it populates _DEFAULT_LOCALE from there. |
Indeed, just importing Although I'd like to point out at a slight distinction between In other words, changing |
How can I set the default locale in Python's factory_boy for all of my Factories?
In docs says that one should set it with factory.Faker.override_default_locale but that does nothing to my fakers...
The text was updated successfully, but these errors were encountered: