My Wiki!

Test Django App

app/test.py

# -*- coding: utf-8 -*-
from django.test import TestCase
 
from django.contrib.auth import get_user_model
from . import models
 
 
class TestProfileModel(TestCase):
 
    def test_profile_creation(self):
        User = get_user_model()
        # New user created
        user = User.objects.create(
            username="taskbuster", password="django-tutorial")
        # Check that a Profile instance has been crated
        self.assertIsInstance(user.profile, models.Profile)
        # Call the save method of the user to activate the signal
        # again, and check that it doesn't try to create another
        # profile instace
        user.save()
        self.assertIsInstance(user.profile, models.Profile)

Note that we’re using the function getusermodel to get the User Model. Again, this is because sometimes we define custom user models, and this functions returns our custom User model if exists, or the Django default otherwise.

Run this test with:

  python manage.py test app
  

All files beginning with 'test' will be checked for test cases.

Test file can be placed in pjsname/pjsname/test.py and run with:

  manage.py test pjs_name

Test runner to test reusable applications


Navigation