backend/django

django model test - model_mommy

seul chan 2018. 2. 26. 11:04

I felt wierd when make testing in django model, so I usally skipped model testing. But I found great library to help testing model, model_mommy

Intall

install with pip

pip install model_mommy

Usage

Basic usage is very simple. Just use with mommy.make(model)

from django.test import TestCase

from model_mommy import mommy

from .models import Post

class PostTestModel(TestCase):

    def setUp(self):
        self.kid = mommy.make(Kid)
    ...

Also can make relational model object.

from django.test import TestCase

from model_mommy import mommy


class CommentTestModel(TestCase):

    def setUp(self):
        self.comment = mommy.make('post.Comment')
        # if using m2m
        self.comment = mommy.make('post.Comment', make_m2m=True)