Django Unit Testing
1 Add Test Suite
cd ~/my_blog_app
. env/bin/activate
cd ~/my_blog_app/blog/blogsite
mkdir tests
cd ~/my_blog_app/blog/blogsite/tests
touch __init__.py
# Model, View for testing
touch test_models.py
touch test_views.py
nano test_models.py
test_models.py
from django.test import TestCase
class ModelsTestCase(TestCase):
pass
2 Test Code
cd ~/my_blog_app/blog/blogsite
nano models.py
models.py
class Post(models.Model):
...
def save(self, *args, **kwargs):
if not self.slug:
self.slug = slugify(self.title)
super(Post, self).save(*args, **kwargs)
...
test_models.py
from django.test import TestCase
from django.template.defaultfilters import slugify
from blogsite.models import Post
class ModelsTestCase(TestCase):
# Test if posts have Slug value
def test_post_has_slug(self):
"""Posts are given slugs correctly when saving"""
post = Post.objects.create(title="My first post")
post.author = "John Doe"
post.save()
self.assertEqual(post.slug, slugify(post.title))
assetEqual
- checks whether slug for post is correct.
- Two Arguments are passed and equality is determined "=="
- raises error if not equal
3 Django's Test Client
test_views.py
from django.test import TestCase
class ViewsTestCase(TestCase):
def test_index_loads_properly(self):
"""The index page loads properly"""
response = self.client.get('your_server_ip:8000')
self.assertEqual(response.status_code, 200)
4 Run Test
cd ~/my_blog_app/blog
python manage.py test
# RESULT
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
..
----------------------------------------------------------------------
Ran 2 tests in 0.007s
OK
Destroying test database for alias 'default'...
참고
https://www.digitalocean.com/community/tutorials/how-to-add-unit-testing-to-your-django-project
Author And Source
이 문제에 관하여(Django Unit Testing), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@csk917work/Django-Unit-Testing저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)