Interface Test essay four based on the Django Web framework for interface testing

Source: Internet
Author: User

Django's fast-developing voting system

Previously shared an example of a Django development voting system. Today in this example to do some extension, speaking of web interface development and testing.

Developing the polling system interface

Although the function of the voting system has been developed, we have not developed a dedicated interface, and in the current polling system, when we call a GET or POST request, the system returns the entire page and returns the test along with the page.

For example, when we want to invoke the interface of all problems (test_get.py)

Import Requestsbase_url = ' http://127.0.0.1:8000/polls ' r = Requests.get (base_url) code = R.status_codetext = R.textprint (code) print (text)

The following results are obtained:

200

the specific interface should return data instead of the entire page, while the data is generally formatted in JSON format. Therefore, the attempt layer (.../polls/views.py) needs to be reformed so that it only provides interfaces and returns data simply.

From django.shortcuts import Render, Get_object_or_404from django.http import Httpresponseredirectfrom Django.core.urlresolvers import reversefrom. Models import Question, Choicefrom django.http import Httpresponseimport json# Create Your views here.# See all Issues def index (request):    latest_question_list = Question.objects.all ()    dicts = {    if latest_question_list: For        question in latest_question_list:            dicts[question.id] = Question.question_ Text        j = json.dumps (dicts)        return HttpResponse (j)    else:        return HttpResponse ("question list null") # View individual problem Options def detail (Request, question_id):    choices = Choice.objects.filter (question_id=question_id)    dicts = {}    print (question_id)    if question_id: for        choice in choices:            dicts[choice.id] = Choice.choice_ Text        j = json.dumps (dicts)        return HttpResponse (j) .....

in order to save time, temporarily first to see all the problems, all the options of a single problem two functions to interface transformation , of course, the transformation is not complete and robust . For example, the interface of all options for a single problem, the received parameter question_id if empty, should be prompted, the parameter is wrong. If the query is not related to the problem, you should be prompted, the query results are empty, if the type is not a number, you should be prompted, type error. These are the processing logic that a robust interface should have.

Execute the test_get.py file again.

200{"1": "\u5341\u4e00\u56fd\u5e86\u4e03\u5929\u5047\u671f\u505a\u4ec0\u4e48\uff1f", "2": "\u4f60\u6700\u60f3\ u5b66\u7684\u81ea\u52a8\u5316\u5de5\u5177\u662f\u4ec0\u4e48\uff1f "}

This time you get the JSON type of data. However, the return value encodes the Chinese Unicode. Here is a little trick to convert it into Chinese.

Open the Firefox browser's Firebug tool and switch to the "Console" tab.

writing an interface document

  

Writing an interface document is also a very important part of the process, because we write an interface that needs to be called by someone else, so how do people know if our interface is called with Get or post? What are the parameters? Of course you need to refer to the interface documentation.

1. Get all questions

Url

Http://127.0.0.1:8000/polls

Request type

Get

Required Parameters

No

return format

Json

return results

{"1": "11 National Day seven days holiday do what?" ",

"2": "What is the most automated tool you want to learn?" "

}

Type of error

No (interface code requires supplemental logic)

2, get all options for a single question

url

http://127.0.0.1:8000/polls/

request type

get

Required parameters

question_id

 

return format

json

 

return result

{"1" :  "Travel",

  "2": "See movie"  ,

  "3": "reading"  

error type

None (interface code requires supplemental logic)

......

All right! The general structure of the interface document is what it looks like. With this document, it's easy to know how to call these interfaces for testing.

System Interface Test

There are two technologies involved in writing interface tests. There is also a brief introduction, UnitTest Unit Test framework and request library.

Import Unittestimport requestsclass pollstest (unittest. TestCase):    def setUp (self):        self.base_url = ' http://127.0.0.1:8000/polls '        def tearDown (self):        Pass        def test_get_poll_index (self):        "Test Polling system home Page" '        r = Requests.get (self.base_url)        code = R.status_code        Text = r.text        self.assertequal (code, $)    def test_get_poll_question (self):        "Get all options for problem 1"        r = requests.get (self.base_url+ '/1/')        code = r.status_code        Text = r.text        self.assertequal (code,        Self.assertin ("3", text) if __name__ = = ' __main__ ':    unittest.main ()

The programming of the interface use case test itself is simple, we only use the calling interface to pass different parameters. This verifies that the return value meets expectations.

Interface Test essay four based on the Django Web framework for interface testing

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.