Web interface Development and testing

Source: Internet
Author: User

Recently, we have been learning and collating web development and interface testing data. Interface testing itself is no more difficult, and there are even a lot of tools and libraries to help us with interface testing. Most testers have a hard time understanding web interface testing because they don't know much about web development, and the more you know about development, the more you'll see what the interface is. Of course, web development is a hassle and it's hard to grasp it all at once.

Note: However, this article is not a 0 basic article, need you to Django Web development, requests interface Library, UnitTest Unit test framework, three have a certain understanding.

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'http://127.0.0.1:8000/polls'== = R.text Print (Code) Print (text)

The following results are obtained:

 $<HTMLLang= "ZH-CN">  <Head>    <Linkhref= "//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"rel= "stylesheet">    <Scriptsrc= "//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></Script>      </Head>  <Body>    <navclass= "NavBar navbar-inverse navbar-fixed-top">      <Divclass= "Container">        <Divclass= "Navbar-header">          <aclass= "Navbar-brand"href="#">Polls System</a>        </Div>      </Div>    </nav>    <BR><BR>   <Divclass= "Well">      <H3>Question List:</H3>                 <ul>             <Li><ahref= "/polls/1/">11 National Day seven days holiday what to do?</a></Li>             <Li><ahref= "/polls/2/">What are the automation tools you want to learn most?</a></Li>          </ul>    </Div>      <Footer>        <P>&copy;Company & Chongshi</P>      </Footer>  </Body></HTML>

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.

 fromDjango.shortcutsImportRender, get_object_or_404 fromDjango.httpImportHttpresponseredirect fromDjango.core.urlresolversImportReverse from. ModelsImportQuestion, Choice fromDjango.httpImportHttpResponseImportJSON#Create your views here.#See all questionsdefIndex (Request): Latest_question_list=Question.objects.all () dicts= {}    iflatest_question_list: forQuestioninchLatest_question_list:dicts[question.id]=Question.question_text J=json.dumps (dicts)returnHttpResponse (j)Else:        returnHttpResponse ("question list null")#View individual problem optionsdefdetail (Request, question_id): Choices= Choice.objects.filter (question_id=question_id) dicts= {}    Print(question_id)ifquestion_id: forChoiceinchChoices:dicts[choice.id]=Choice.choice_text J=json.dumps (dicts)returnHttpResponse (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 the options for a single question

Url

http://127.0.0.1:8000/polls/

Request type

Get

Required Parameters

question_id

return format

Json

return results

{"1": " travel ",

"2": " watch the movie ",

"3": " reading "

}

Type of error

No (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.

ImportUnitTestImportRequestsclasspollstest (unittest. TestCase):defsetUp (self): Self.base_url='Http://127.0.0.1:8000/polls'        defTearDown (self):Pass        defTest_get_poll_index (self):" "Test voting system home" "R=requests.get (self.base_url) code=R.status_code Text=r.text self.assertequal (code,200)    deftest_get_poll_question (self):" "get all the options for issue 1" "R= Requests.get (self.base_url+'/1/') Code=R.status_code Text=r.text self.assertequal (code,200) 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.

Web interface Development and 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.