[PYTHON] Django Note 5

9 Django Test

9.1 Introduction to the database

When testing, prepare a new database for testing instead of using an existing database. After running all the tests, the database will be deleted. When running test in the shell, you can add the -keepdb parameter to leave the test database.

9.2 How to write

For example

models.py


from django.db import models
import django.utils.timezone as timezone

class File(models.Model):
    username = models.CharField(max_length=200)
    filename = models.CharField(max_length=200)
    fileshortname = models.CharField(max_length=200)
    filetype = models.CharField(max_length=200)
    filedata = models.FileField(upload_to='files')
    filepic = models.ImageField(null=True, upload_to='covers')
    uploadtime = models.DateTimeField('updated time', default = timezone.now)
    tags = models.ManyToManyField(Tag)

    def __str__(self):
        return self.filename

    def setFileType(self):
        if self.filename is None:
            self.filetype = None
        else:
            e = self.filename.split('.')[-1]
            self.filetype = e

tests.py


from django.test import TestCase
from models import File

class FileTestCase(TestCase):
    def test_setfiletype_when_can_not_Splited(self):
        testfileobj = File.objects.create(filename='testfile')
        testfileobj.setFileType()
        self.assertIsNone(testfileobj.fileshortname)

The Django TestCase class inherits from Python.unittest, so use assertEqual, assertTrue, assertIs, etc. to write your test code.

9.3 Django View Test Tool

tests.py


from django.test import TestCase
from models import File, Tag

class FileTestCase(TestCase):
    ...
    def test_no_file(self):
        response = self.client.get('/')
        self.assertEqual(response.status_code, 200)
    def test_upload_large_file(self):
        #Prepare in advance
        with open('testfile.dat') as fd
        response = self.client.post('upload/', {'filedata':fd, 'fileshortname':'test', 'tags': 'test'})
        self.assertEqual(response.status_code, 200)

You can use the client to check a simple view.

Recommended Posts

Django note 4
Django Note 5
Django Note 1
Django note 3
Django note 2
Note
Note
Note
Django Girls Tutorial Note
(Note) Django in Vagrant environment
[Note] [For myself] Django command
django update
pyenv note
[Note] Django project creation and terminology
Django memorandum
django search
Django installation
Django Summary
Django test
Django # 2 (template)
Django hands-on
Note: Python
Touch django
django notes
Django Summary
Django basics
Django Shoho
Django defaults
Ansible Note
Django + Docker
Django Glossary
Python note
Django search
Install Django
[Note] Run Django on Amazon Linux 2
Django: References
Note: Send an email with Django
direnv note
Django startup
Django notes
[Note] RepresenterError
Django NullCharField
Deploy Django api on heroku (personal note)
(Note) Template file search order in Django
A note on enabling PostgreSQL with Django
DJango Note: From the beginning (form processing)
Django environment construction
[Note] Image resizing
Python study note_002
Django ~ settings.py edition ~
Note: Python Decorator
Python programming note
Django Heroku Deploy 1
[Python] Learning Note 1
Django HTML Template # 2
Django Contact Form 2
Django begins part 1
Django model: ManyToManyField
What is Django? .. ..
Models in Django
Django function-based view