Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Factoryboy: Creating data for unit tests in an ...

Factoryboy: Creating data for unit tests in an easy way

Factoryboy is a Python library that replaces fixtures, useful when writing unit tests and some generated data is needed.

Andrea Grandi

January 27, 2014
Tweet

More Decks by Andrea Grandi

Other Decks in Programming

Transcript

  1. HOW TO GET DATA FOR UNIT TESTS • Using production

    DB – Do NOT do it! • Using fixtures – Not very handy • Using the ORM way to create data – It's ok but it can be long, repetitive and increase the lines of code in unittests • Or ...
  2. FACTORYBOY • Support for multiple build strategies (unsaved/saved instances: build()

    and create() ) • Powerful helpers for common cases (sequences, sub-factories, reverse dependencies, circular factories, ...) • Support for various ORMs (currently Django, Mogo, MongoEngine, SQLAlchemy) • Python >= 2.6 and PyPy are supported
  3. FACTORYBOY: DEFINING FACTORIES • subclass factory.Factory • add a class

    Meta block • set the model attribute of the Meta block • add default values
  4. FACTORYBOY: DEFINING FACTORIES (EXAMPLE) import factory from . import models

    class CustomerFactory(factory.Factory): class Meta: model = models.Customer first_name = 'John' last_name = 'Doe' admin = False
  5. FACTORYBOY: USING FACTORIES # Returns a Customer instance that's not

    saved customer = CustomerFactory.build() # Returns a saved Customer instance customer = CustomerFactory.create() # Returns a dict of attributes that can be used to # build a Customer instance attributes = CustomerFactory.attributes()
  6. YOU CAN ALWAYS OVERRIDE # Build a Customer instance and

    override first_name >>> customer = CustomerFactory.build(first_name='Joe') >>> customer.first_name "Joe"
  7. Lazy Attributes class CustomerFactory(factory.Factory): class Meta: model = models.Customer first_name

    = 'Joe' last_name = 'Blow' email = factory.LazyAttribute( lambda a:'{0}.{1}@example.com'.format( a.first_name, a.last_name).lower())
  8. Sequences class CustomerFactory(factory.Factory): class Meta: model = models.Customer email =

    factory.Sequence( lambda n: person{0}@example.com'.format(n)) >>> CustomerFactory().email '[email protected]' >>> CustomerFactory().email '[email protected]'
  9. Create Strategy # Builds and saves a Customer and a

    Post >>> post = PostFactory.create() >>> post.id is None # Post has been 'saved' False >>> post.author.id is None # post.author has been saved False
  10. Build Strategy # Builds but does not save a Customer,

    and then builds # but does not save a Post >>> post = PostFactory.build() >>> post.id is None True >>> post.author.id is None True
  11. ORM Support • Django, with DjangoModelFactory • Mogo, with MogoFactory

    • MongoEngine, with MongoEngineFactory • SQLAlchemy, with SQLAlchemyModelFactory
  12. References All the code examples and documentation have been taken

    from the official project website, available at http://factoryboy.readthedocs.org