Herramientas de usuario

Herramientas del sitio


otros:factoryboy

¡Esta es una revisión vieja del documento!


Factory Boy

Introduction

What is Factory Boy?

Factory Boy aim is to replace static and hard to maintain complex object fixtures (test data).

This is test data created with factory boy (We need a 200€, paid order, shipping to australia, for a VIP customer):

def test_with_factory_boy(self):
	order = OrderFactory(
		amount=200,
		status='PAID',
		customer__is_vip=True,
		address__country='AU',
	)

This is data created without factory boy:

def test_without_factory_boy(self):
	address = Address(
		street="42 fubar street",
		zipcode="42Z42",
		city="Sydney",
		country="AU",
	)
	customer = Customer(
		first_name="John",
		last_name="Doe",
		phone="+1234",
		email="john.doe@example.org",
		active=True,
		is_vip=True,
		address=address,
	)

Installation

$ pip install factory_boy

ORMs

MongoEngine

from factory.mongoengine import MongoEngineFactory
# ...
class User(Document):
# ...
class UserFactory(MongoEngineFactory):

First steps

Factories

Factories declare a set of attributes for an object. The class of this object must be defined in the model attribute of a subclass Meta:

import factory
import models
 
class UserFactory(factory.Factory):
    class Meta:
        model = models.User
 
    first_name = 'John'
    last_name = 'Doe'
    admin = False

:!: In previous versions the class Meta with its attribute model it is substituted by FACTORY_FOR attribute.

otros/factoryboy.1400747632.txt.gz · Última modificación: 2020/05/09 09:25 (editor externo)