Interface automated testing unittest + request + excel (a)

Note:

  Learning python automated testing, you need to learn python basis, mainly knock codes, multi-link, Practice makes perfect, you will be a qualified programmer

  python based learning:

    http://c.biancheng.net/python/

    https://www.runoob.com/python3/python3-tutorial.html

Write an interface automated testing, you have to understand his master's library, such as the two most important libraries, unittest and requests the following;

  The official document: https://docs.python.org/3/library/unittest.html     unittest

  The official document: http://2.python-requests.org/zh_CN/latest/user/quickstart.html   Requests

Master these two libraries, I believe that to do interface testing is not in question.

 

Interface Test post (Interface Test There are a variety of writing, it can be successfully OK)

 1 # coding:utf-8
 2 import unittest,time 
 3 import requests
 4 import re
 5 
 6 host = "http://192.168.0.175:8080"
 7 
 8 
 9 def login(s,username,psw):
10         url = host+"/login.jsp"
11 
12         headers = {
13         "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0",
14         "The Accept " : " text / HTML, file application / XHTML + XML, file application / XML; Q = 0.9, * / *; Q = 0.8 " ,
 15          " the Accept-Language " : " ZH-the CN, ZH; Q = 0.8, EN -US; Q = 0.5, EN; Q = 0.3 " ,
 16          " the Accept-Encoding " : " the gzip, the deflate " ,
 . 17          " the Referer " : Host + " /zentao/user-login.html " ,
 18 is          # " cookies ": # head before did not pass login cookie, because here is to keep the cookie log 
19          " Connection ": "keep-alive",
20         "Content-Type": "application/x-www-form-urlencoded",
21         }
22 
23         data = {'os_username': 'admin',
24                 'os_password': '123456',
25                 'os_destination': '',
26                 'user_role': '',
27                 'atl_token ' : ' ' ,
 28                  ' Login ' : ' % E7 E5%%% 99% BB 95% the BD ' 
29                  }
 30  
31 is          # S = requests.session () Do not write the dead the session 
32  
33 is          R1 = s.post (URL , the Data = the Data, headers = headers)
 34          return r1.content
 35  
36  DEF is_login_sucess (RES):
 37          iF  " Login failed, Please check your user name or password correctly filled. "  in RES:
 38                  return False
 39          elif  "parent.location=" in res:
40                 return True
41         else:
42                 return False
43 
44 if __name__ == "__main__":
45         s = requests.session()
46         a = login(s,"admin","123456")
47         print(is_login_sucess(a))    

 

Package interface: post and get (to build the interface automated testing framework, encapsulation can be more convenient)

import requests
import json
class RunMain:
    def send_get(self, url, data):
        res = requests.get(url=url, params=json.dumps(data).json()
        return res

    def send_post(self, url, data):
        res =requests.post(url=url, data=json.dumps(data)).json()
        return res

    def run_main(self, url, method, data=None):
        if method == 'POST':
            res = self.send_post(url, data)
        else:
            res = self.send_get(url, data)
        return res

if __name__ == "__main__":
    url = 'http://192.168.0.157:18005/oauth/token'
    data = {
            'clientId': 'ut.usscity.com',
            'password': '123456',
            'userName': 'admin',
            'VerificationCode': '',
            'VerificationCodeKey': "f7dc3967-bfbc-4a0f-9e2d-4d6e403d10a1"
        }
    test = RunMain()
    print(test.run_main(url, 'POST', data))

Operational parameters can be derived return

 

 Return parameters can also be landscaped, just as Navicat

class RunMain:
    def send_get(self, url, data):
        res = requests.get(url=url, params=data).json()
        #return res
        return json.dumps(res, indent=2, sort_keys=False, ensure_ascii=False)

    def send_post(self, url, data):
        res =requests.post(url=url, data=json.dumps(data)).json()
        #return res
        return json.dumps(res,indent=2, sort_keys=False, ensure_ascii=False)

  The result is returned parameters

 

 

 Learn a little every day, the future is yours

 

Guess you like

Origin www.cnblogs.com/hemingwei/p/11543243.html