Using Python for interface testing

Source: Internet
Author: User
Tags urlencode

Recently in the interface test, the company's plan was to use postman for interface testing. But the Great Wall caused us to only use the offline version of Postman. Then a very long and long list of interfaces, one after another access. Oh, my God. So an idea arose, using Python to write a set of interface test scripts, set up the interface list, and then go through each of the output logs.

First pit: Post and get----get is typically used to get/query resource information, and post is typically used to update resource information | Get is a request to send data to the server, and post is a request to submit data to the server.

The person who has done the interface test or the front end knows that the interface access is inconsistent, so the postman is used to test the interface, because it can set the post and get mode. Using Python to simulate these two types of access is a priority. Say get mode first. The Get method is relatively simple, put the interface into the browser address bar, the next car to complete a get. So you need to use Python to access the URL to simulate a get test.

1 Import urllib22 url_save = ' http://www.baidu.com/' 3 try:4     s_save = Urllib2.urlopen (url_save). Read () 5     print S_ Save  6 except Urllib2. Httperror, e:7     print e.code8 except Urllib2. Urlerror, e:9     print str (e)

A GET request is completed as shown above, the URLLIB2 library is called, and a string-like URL is passed to the Urllib2.urlopen function, and finally the get-back data is stored using the Read () method.

Then talk about post. In fact, in Python's urllib2 library, the Urlopen function we just used has a few other things that are not required, because these parameters are given the initialized values:

1 def urlopen (URL, data=none, timeout=socket._global_default_timeout,2             cafile=none, Capath=none, Cadefault=False , Context=none):

As the code above, the Urllib library has a very intelligent problem. Data does not give a value, the access method is Get,data gives the value, the way will become post, so the code to simulate the post is as follows:

Import urllib import urllib2 url = ' http://www.example.com ' # The form of values: Name:valuevalues = {' * * ': ' * * * ',           ' * * ': ' * * * ' ,           ' * * ': ' * * * '} #使用urllib. The UrlEncode function processes the values dictionary in the final form: **=***&**=***data = Urllib.urlencode (values) # If the data order is required, it is recommended that you splice datareq = urllib2. Request (URL, data) response = Urllib2.urlopen (req) the_page = Response.read ()

Like the code above, the Post method is written to the data parameter, and the post is simulated successfully.

Second pit: Use of cookies

The library required to get a cookie using Python is called cookielib. Examples of obtaining cookies:

1 # There are four kinds of cookiejar,cookiejar that are the most primitive 2 Cookie_use = Cookielib. Cookiejar () 3 handler = Urllib2. Httpcookieprocessor (Cookie_use) 4 # Create a opener 5 opener = Urllib2.build_opener (handler) 6 # Using handler bindings good Cookiejar Install opener into URLLIB2 7 Urllib2.install_opener (opener) 8 # Use the installed URLLIB2 to access a website to get cookies 9 urllib2.urlopen (' https://... /login ') #这个时候cookie已经被CookieJar获取到了11 print Cookie_use

In the next step, bind the obtained cookie to the opener header:

1 "2  will get the cookie bound to opener, the previous step to obtain the cookie does not meet the following format, 3  need to do their own string slicing and stitching 4" ' 5 opener.addheaders.append (' Cookies ', ' name=***&888=888 '))

Now the opener can be used to access any website that needs to login!

Transfer from http://www.cnblogs.com/hainan-zhang/p/5320153.html#undefined

                                    

Using Python for interface testing

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.