Python third-party library series 18--python/django test Library

Source: Internet
Author: User

Django is a web framework for Python voice, and for Django testing you can also talk about Python testing first. Django can be tested in Python, and, of course, Django encapsulates a test library of its own based on Python.
First, the Python Test--unitest Library
def my_func (a_list, idx):    return A_list[idx]    import unittestclass myfunctestcase (unittest. TestCase):    def testbasic (self):        a = [' Larry ', ' Curly ', ' Moe ']        self.assertequal (My_func (A, 0), ' Larry ')        self.assertequal (My_func (A, 1), ' Curly ')
Second, Django Test--django.utils.unittest Library, Django.test.client Library.
(1) Django.utils.unittest Library
A higher-level reference to the Django-encapsulated unittest. By the way, there is a benefit to writing this: The test case can be used directly with Django's models, and it is very convenient to operate the database.
From django.utils import unittestfrom myapp.models import Animalclass animaltestcase (unittest. TestCase):    def setUp (self):        self.lion = Animal.objects.create (name= "Lion", sound= "Roar")        Self.cat = Animal.objects.create (name= "Cat", sound= "Meow")    def Test_animals_can_speak (self): "" "        animals that can speak is correctly identified "" "        self.assertequal (Self.lion.speak (), ' The lion says ' Roar ')        self.assertequal ( Self.cat.speak (), ' the cat says ' Meow "')
(2) Django.test.client Library
To test its service capabilities, Django, as the Web server, must impersonate a client to access server-side validation
From django.utils import unittestfrom django.test.client import Clientclass simpletest (unittest. TestCase):    def setUp (self):        # Every Test needs a client.        self.client = Client ()    def test_details (self):        # Issue a GET request.        Response = Self.client.get ('/customer/details/')        # Check that the response is OK.        Self.assertequal (Response.status_code, $)        # Check that the rendered context contains 5 customers.        Self.assertequal (Len (response.context[' customers '), 5)
Or more advanced, directly with the from Django.test.TestCase
From django.test import Testcaseclass simpletest (TestCase):    def test_details (self):        response = Self.client.get ('/customer/details/')        self.assertequal (Response.status_code, $)    def test_index (self):        response = self.client.get ('/customer/index/')        self.assertequal (Response.status_code, 200)
Third, the use of the database

If you still need to initialize the database when you test, the Testcase.fixtures help you.

Also, Django will help you create a new database name: Test_ The original database name when you test it. This prevents the real database from being soiled.

You can initialize your database in this way:
From django.test import testcasefrom myapp.models import Animalclass animaltestcase (TestCase):    fixtures = [' Mammals.json ', ' birds ']    def setUp (self):        # Test definitions as before.        Call_setup_methods ()    def testfluffyanimals (self):        # A Test that uses the fixtures.        Call_some_test_code ()
Then someone asked: How did Mammals.json come? What is the format? In fact, this file is a Python manage.py dumpdata command, the document about this command please Google. The file format is probably as follows:
[    {        "pk": 1,        "model": "Apps.animal",        "fields": {            "status": 1,            "gmt_modified": " 2015-07-06t14:05:38 ",            " gmt_created ":" 2015-07-06t14:05:38 ",            " Alive ": 1,            " info ":"        }    },    {"        pk": 2,        "model": "Apps.animal",        "fields": {            "status": 0,            "gmt_modified": " 2015-07-06t14:05:53 ",            " gmt_created ":" 2015-07-06t14:05:53 ",            " Alive ": 1,            " info ":" "        }    }]
In fact, django.test This package also has a lot of other functions, can not be enumerated, in detail, see the official Django document.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Python third-party library series 18--python/django test Library

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.