I am considering using the factory_boy library for API testing. An example from the documentation is:
class UserFactory(factory.Factory):
class Meta:
You could pack the __init__
method's keyword arguments into one dict, and set them dynamically with setattr
:
class User(object):
GENDER_MALE = 'mr'
GENDER_FEMALE = 'ms'
def __init__(self, **kwargs):
valid_keys = ["title", "first_name", "last_name", "is_guest", "company_name", "mobile", "landline", "email", "password", "fax", "wants_sms_notification", "wants_email_notification", "wants_newsletter","street_address"]
for key in valid_keys:
setattr(self, key, kwargs.get(key))
x = User(first_name="Kevin", password="hunter2")
print(x.first_name, x.password, x.mobile)
However, this has the drawback of disallowing you from supplying arguments without naming them - x = User("Mr", "Kevin")
works with your original code, but not with this code.