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

Source: Internet
Author: User

Django is a web framework that belongs to Python voice, to say Django Test. You can also talk about Python's test first. Django can test it in Python, and of course, Django also 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, this is another advantage: test case can be directly used in Django models, the operation of the database is very convenient.


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
As a Web server, Django has to test its service function by simulating a client and interviewing the server for 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 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

Suppose you need to initialize the database when you test, that testcase.fixtures help you.

And Django is testing it. will help you create a new database named: Test_ the original database name.

This will prevent the real database from getting dirty.

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 in the Python manage.py dumpdata command. For a document on this command, please Google. The file format is probably for example the following:
[    {        "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 has a lot of other functions, can not be enumerated, specifically to see the official Django document.

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.