question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Change default faker locale in factory_boy

See original GitHub issue

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…

import factory
from app.models import Example
from custom_fakers import CustomFakers

# I use custom fakers, this indeed are added
factory.Faker.add_provider(CustomFakers)
# But not default locales
factory.Faker.override_default_locale('es_ES')

class ExampleFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = Example

    name = factory.Faker('first_name')


>>> from example import ExampleFactory
>>> e1 = ExampleFactory()
>>> e1.name
>>> u'Chad'

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:2
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
x-yuricommented, Apr 6, 2019

See workaround here.

4reactions
rbarroiscommented, Jan 4, 2021

So, let’s look at the different situations where one might want to change the default locale:

  • For part of a specific test: this is covered by with override_default_locale():
  • For a specific test function: this is covered by @override_default_locale()
  • For a collection of tests grouped in a TestCase subclass: @override_default_locale() might not be enough here
  • For a whole run of the program / test suite, with a fixed value from run to run
  • For a whole run of the program / test suite, with different values between runs

For the last two cases, we’ll have to look into the program setup:

  • For a program (say, generating a realistic dataset), it is usually easy to add a with override_default_locale() within the program’s main() function
  • For a test run, this has to happen within the test runner setup

For Django, I believe that the recommended mode is to:

  1. Subclass Django’s DiscoverRunner
  2. Overload its run_tests function, to wrap the initial method inside a with override_default_locale()
  3. Point your settings’ TEST_RUNNER to your class:
# 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)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Change default faker locale in factory_boy - Stack Overflow
In docs says that one should set it with factory.Faker.override_default_locale but that does nothing to my fakers... import factory from app.
Read more >
[Answered]-Change default faker locale in factory_boy-django
In docs says that one should set it with factory.Faker.override_default_locale but that does nothing to my fakers... import factory from app.models import ...
Read more >
Reference — Factory Boy stable documentation
Change the default strategy of the decorated Factory to the chosen strategy : ... In order to easily define realistic-looking factories, use the...
Read more >
Locale en_US — Faker 15.3.4 documentation
If safe_mode is True (default), this method performs another set of conversions to guarantee that the UPC-E barcodes generated can be converted to...
Read more >
factory.faker — Factory Boy stable documentation
Faker ('name') """ import contextlib import faker import faker.config from . import ... Args: provider (str): the name of the Faker field locale...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found