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
from django.test import TestCase
class ModelsTestCase(TestCase):
pass
cd ~/my_blog_app/blog/blogsite
nano 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)
...
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))
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)
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