Django unit test (1), Django unit test (

Source: Internet
Author: User

Django unit test (1), Django unit test (

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

Writing tests

Unit Tests of Django use the unittest module of python, which defines tests using class-based methods. Class Name: django. test. TestCase, inherited from unittest. TestCase of python.

from django.test import TestCasefrom myapp.models import Animalclass 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"')

During the test, the test program searches for all test cases (subclasses of inittest. TestCase) in all files starting with test, automatically creates a test set, and then runs the test.

Note:: If the test is based on database access (read and query Model), you must use django. test. TestCase to create a test class instead of unittest. TestCase.

Runing tests

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

$ python manage.py test

Run the test in the tests package under the animals project:

$ python manage.py test animals.tests

Run the test in the animals project:

$ python manage.py test animals

Run a test case separately:

$ python manage.py test animals.tests.AnimalTestCase

Run a test method separately:

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

Provide the path for the 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

The test requires a database. django will generate a separate database for the test. Whether or not your test passes, the database will be destroyed after all your tests are executed.

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

In addition to creating databases separately, the test tool uses the same database configuration-DATABASE_ENGINE, DATABASE_USER, DATABASE_HOST, and so on. the user DATABASE_USER (settings) who created the test database is specified, so you need to confirm that DATABASE_USER has sufficient permissions to create the database.

Test execution sequence

To ensure that all tests start from a clean database, the execution sequence is as follows:

1. All TestCase subclasses are run first.

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

3. Other tests (such as doctests)

Acceleration test

You can set PASSWORD_HASHERS to a faster algorithm:

PASSWORD_HASHERS = (    'django.contrib.auth.hashers.MD5PasswordHasher',)
 
In a unit test of mathematics, the scores of the eight children were 75, 90, 85, 90, 95, 90, 95, and 92 respectively.

Mode (90)
Median (90)
The ratio of height is)
High is (30)
(√)
(×)
(×)
Available for (90) days
(B)
()
31.5-36 usd16
= 31.5-2.25
= 29.25

(6-1.3 × 4) limit 0. 1
= (6-5.2) defaults 0. 1
= 5.8 limit 0. 1
= 58

[(4/5-2/3) × 5/8] limit 5/7
= (2/15 × 5/8) limit 5/7
= 1/12 limit 5/7
= 7/60

Well received

How to query a column in column 1 in django is the same as select name from

Xxx. objects. get () is used to query the column with the id of 5 of the Car object.
Car. objects. get (id = 5) is worth noting that the get () method can only be used to query one column. If no or more results are returned, an error is returned. If you are not sure, use Car. objects. the list returned by the filter (id = 5). It can be null. If there is only one entry, the first entry is complete.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.