Search code examples
djangofactory-boy

specify multiple variations for field in database using factory boy


I am new to using the tool django factory boy. I would like to know how can we specify multiple test data patterns for a field in database model using the factory class of django factory boy.

For eg :Username is a field in the table users, username can have variations like abc_123,abc123.


Solution

  • FactoryBoy provides a few options for this:

    • Using the hook to the Faker library, which includes rules to generate realistic values for common fields. Calling factory.Faker('user_name') will provide, for instance: bmiller, ogarcia, alee;
    • The factory.fuzzy module provides some helpers for (managed) random generation. Calling factory.fuzzy.FuzzyText(prefix='abc_') would generate abc_aCybLgKHOpdI or abc_pCaddoWBZbEO;
    • You may design your own fuzzy generator, inspired on the code in factory/fuzzy.py;
    • You could build the usernames from the sequence counter provided to factory.Sequence