I would like to create my own customer Faker provider.
I am using factory-boy which comes already with Faker included, so in my test factories I am using for a UserFactory
name = factory.Faker('name')
My question is, can I somehow implement my own custom provider? So I could use factory.Faker('my_provider')? Or for that I would have to swap all factory.Faker() instances and just use Faker() instance?
You can inherit the
BaseProviderclass and define your custom functions. Afterwards you can add it to aFakerobject.There are two possible options:
1. Retrieve factory Faker object from factory library and add custom provider.
In this scenario you retrieve the instantiated
Fakerobject from thefactorylibrary using a private get method. Please note, it is bad practice to access private methods outside of a class.2. Instantiate new Faker object and add custom provider
With this option you achieve the same result without accessing any private methods outside of the class. I would advise on this course of action.
Faker BaseProvider reference