Flexible Creates in Testing
Here’s a syntax that I used quite frequently when doing unit tests in Ruby. A similar syntax in Python works quite well once you get past the death star syntax:
def create_category(self, **options):
return Category.objects.create(**dict({'name': 'Python', 'description': 'Python rocks, mmmkay'}, **options))
By setting up the above method / function in your tests you can then use it as a default by just calling the function. But if you want to override a particular aspect of the create, for instance to set a required field to None, you can just pass that in easily with:
create_category(name=None):
It works quite well and makes it easy to provide defaults around which you modify to test certain aspects of your code.
