Django unit testing (1)

高洛峰
Release: 2016-10-17 13:53:45
Original
1134 people have browsed it

Django testing framework is very simple, the preferred method is to use the unittest module in the python standard library.

Writing tests

Django’s unit testing uses python’s unittest module, which uses a class-based approach to define tests. The class name is django.test.TestCase, which is inherited from python’s unittest.TestCase.

from django.test import TestCase
from myapp.models import Animal
  
class AnimalTestCase(TestCase):
    def setUp(self):
        Animal.objects.create(name="lion", sound="roar")
        Animal.objects.create(name="cat", sound="meow")
  
    def test_animals_can_speak(self):
        """Animals that can speak are correctly identified"""
        lion = Animal.objects.get(name="lion")
        cat = Animal.objects.get(name="cat")
        self.assertEqual(lion.speak(), 'The lion says "roar"')
        self.assertEqual(cat.speak(), 'The cat says "meow"')
Copy after login

When running a test, the test program will search for all test cases (subclasses of inittest.TestCase) in all files starting with test, automatically create a test set and then run the test.

Note: If the test is based on database access (reading, querying Model), be sure to use django.test.TestCase to create the test class instead of unittest.TestCase.

Runing tests

Execute all tests in the directory (all test*.py files):

$ python manage.py test

Execute the tests in the tests package under the animals project:

$ python manage.py test animals.tests

Execute the test test in the animals project:

$ python manage.py test animals

Execute a test case individually:

$ python manage.py test animals.tests.AnimalTestCase

Execute a test case individually Test method:

$ python manage.py test animals.tests.AnimalTestCase.test_animals_can_speak

Provide path to test file:

$ python manage.py test animals/

Wildcard test file name:

$ python manage. py test --pattern="tests_*.py"

Enable warnings reminder:

$ python -Wall manage.py test

Database

Testing requires a database, and django will generate a separate database for the test. Regardless of whether your tests pass or not, when all your tests have been executed, the test database will be destroyed.

By default, the name of the test database is test_DATABASE_NAME, and DATABASE_NAME is the database name you configured in settings.py. If you need to give the test database another name, specify the value of TEST_DATABASE_NAME in settings.py. When using sqlite3, the database is created in memory.

Except that the database is created separately, the test tool will use the same database configuration - DATABASE_ENGINE, DATABASE_USER, DATABASE_HOST, etc. The user who created the test database is specified by DATABASE_USER (in settings), so you need to confirm that DATABASE_USER has sufficient permissions to Create database.

Test execution order

In order to ensure that all tests start from a clean database, the execution order is as follows:

1. All TestCase subclasses run first.

2. All other unit tests (unittest.TestCase, SimpleTestCase, TransactionTestCase).

3. Other tests (such as doctests, etc.)

Accelerated testing

You can set PASSWORD_HASHERS to a faster algorithm:

PASSWORD_HASHERS = (
    'django.contrib.auth.hashers.MD5PasswordHasher',
)
Copy after login


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template