GithubHelp home page GithubHelp logo

mommys_boy's Introduction

If you want to create test fixtures for your Django project there are two excellent options: Model Mommy <http://model-mommy.readthedocs.io/> and Factory Boy <http://model-mommy.readthedocs.io/>, which were inspired in their Ruby counterparts Object Daddy <https://github.com/flogic/object_daddy> and Factory Girl <https://github.com/thoughtbot/factory_girl>.

While Model Mommy feels lighter and requires absolutely no boilerplate for the simple cases, Factory Boy is more flexible and have more advanced features that might be necessary some complex use cases. Which one is better for your project? Why not both? ;-)

Enters Mommy's Boy

Mommy's Boy uses both Model Mommy and Factory Boy under the hood so you can benefit from both libraries and choose the interface that is more suitable to each case.

For really simple needs, we can use the make() and prepare() functions of Model Mommy's API:

>>> from mommys_boy import mommy
>>> from django.contrib.auth.models import User
>>> user = mommy.make(User, first_name='John')

Mommy's boy leverages both Fake Factory and Model Mommy to automatically fill up your model's fields. It tries to use meaningful values by matching a field's name to the corresponding Fake Factory <http://faker.readthedocs.io/> function. You can also pass explicit values such as first_name='John' in our example.

>>> user.first_name
'John'
>>> user.last_name  # Chosen randomly from the fake.last_name() function
'McLovin'

Model Mommy fills any required field that does not match a function in Fake Factory with random data.

FactoryBoy Integration

We can also use Fake Factory introspection and Model Mommy's abilities within a Factory Boy factory:

from mommys_boy import DjangoMommyFactory
from django.contrib.auth.models import User


class UserFactory(DjangoMommyFactory):
    class Meta:
        model = User

Now we can use the .create() and .build() functions to create instances of the User class.

>>> user = UserFactory.create(email='[email protected]')
>>> user.email
'[email protected]'
>>> user.first_name  # this field were automatically filled by Fake Factory
'Paul'

mommys_boy's People

Contributors

fabiommendes avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.