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)
'backend > django' 카테고리의 다른 글
django admin을 잘 쓰자! - 장고 어드민 cookbook (0) | 2018.03.13 |
---|---|
django test plus (0) | 2018.02.27 |
django hitcount: counting hit (views) in django (0) | 2018.02.20 |
django: reversed forloop in template (0) | 2018.02.19 |
django: using queryset chainable model manager (0) | 2018.02.17 |