亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python+requests+pytest接口自動(dòng)化的實(shí)現(xiàn)示例

 更新時(shí)間:2021年04月16日 10:51:44   作者:今天學(xué)習(xí)嗎  
這篇文章主要介紹了python+requests+pytest接口自動(dòng)化的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

1、發(fā)送get請(qǐng)求

#導(dǎo)包
import requests
#定義一個(gè)url
url = "http://xxxxxxx"
#傳遞參數(shù)
payload="{\"head\":{\"accessToken\":\"\",\"lastnotice\":0,\"msgid\":\"\"},\"body\":{\"user_name\":\"super_admin\",\"password\":\"b50c34503a97e7d0d44c38f72d2e91ad\",\"role_type\":1}}"
headers = {
  'Content-Type': 'text/plain',
  'Cookie': 'akpsysessionid=bafc0ad457d5a99f3a4e53a1d4b32519'
}
#發(fā)送get請(qǐng)求
r = requests.get( url=url,headers=headers, data=payload)
#打印結(jié)果
print(r.text)
#解碼
print(r.encoding)
print(r.text.encode('utf-8').decode('unicode_escape'))#先把返回的結(jié)果轉(zhuǎn)換成utf-8,再去解碼成中文的編碼

2、發(fā)送post請(qǐng)求

#導(dǎo)包
import requests
#定義一個(gè)url
url = "http://xxxxxxx"
#傳遞參數(shù)
payload="{\"head\":{\"accessToken\":\"\",\"lastnotice\":0,\"msgid\":\"\"},\"body\":{\"user_name\":\"super_admin\",\"password\":\"b50c34503a97e7d0d44c38f72d2e91ad\",\"role_type\":1}}"
headers = {
 'Content-Type': 'text/plain',
 'Cookie': 'akpsysessionid=bafc0ad457d5a99f3a4e53a1d4b32519'
}
#發(fā)送post請(qǐng)求
r = requests.post( url=url,headers=headers, data=payload)
#打印結(jié)果
print(r.text)

3、發(fā)送https請(qǐng)求

import requests
url='https://www.ctrip.com/'
#第一種解決方案,發(fā)送請(qǐng)求的時(shí)候忽略證書(shū),證書(shū)的參數(shù)verify用的比較多
r=requests.post(url=url,verify=False)#verify參數(shù)默認(rèn)為T(mén)rue,值為False,表示忽略證書(shū)
#第二張解決方案,verify里面添加證書(shū)的路徑
r=requests.post(url=url,verify='證書(shū)的路徑')#verify參數(shù)默認(rèn)為T(mén)rue,值為False,表示忽略證書(shū)
print(r.text)

4、文件上傳

import requests
file = {
    'filename':open('文件名稱(chēng)','rb')
}
response = requests.post("網(wǎng)址",file)
print(response.text)

5、文件下載

#小文件下載
import requests
r = requests.get("https://img.sitven.cn/Tencent_blog_detail.jpg")
with open(r"D:\a.jpg", "wb") as f:
    f.write(r.content)

#大文件下載
import requests
def test_downloads(url, file):
    s = requests.session()
    r = s.get(url, stream=True, verify=False)
    with open(file, "wb") as f:
        for chunk in r.iter_content(chunk_size=512):
            f.write(chunk) 
if __name__ == "__main__":
    url = "https://www.url.com/test/export"
    file = "D:\\a.xlsx"
    test_downloads(url=url, file=file)

6、timeout超時(shí)

#導(dǎo)包
import requests
#循環(huán)10次
for i in range(0,10):
    try:
        url="http://xxxxxxxxxxxxxxxx"
        data={
            "head":{"lastnotice":0,"msgid":"","accessToken":"89a08bff-15d7-4d7a-9967-0b5f4fb699ce"},
            "body":{"clinicid":"978f661e-1782-43bd-8675-b0ff1138ab7c","deptid":"09b8515b-b01b-4771-9356-aed6b5aa01bf","doctorid":"65ac0251-10ff-473a-af8a-20e8969176f7","registtype":0,"card_num":"","bcc334":"","patientopt":1,"bkc368":"1","patient":{"cardid":"","medicalcardid":"","label":"","sourcetype":1,"nationid":"01","maritalstatus":0,"address":"","company":"","jobname":"","email":"","remark":"","bcc334":"","name":"11","gender":1,"phone":"","birthdate":"2020-03-23","patienttype":1,"szsbcardid":""}}
        }
#發(fā)送post請(qǐng)求,超時(shí)時(shí)間0.03s
        r=requests.post(url=url,json=data,timeout=0.03)
        print(r.text)
        print(r.cookies)
    except:
        print('error')

7、鑒權(quán)

7.1、auth參數(shù)鑒權(quán)

import requests
url = 'http://192.168.1.1'
headers = {} # 有的不帶頭也能請(qǐng)求到 不帶頭可以忽略這行 和headers=headers,這兩處
r = requests.get(url, auth=('admin', '123456'), headers=headers, timeout=10)
print(r.text)

7.2、session操作

#實(shí)例化session
session = requests.session()
#使用session發(fā)起請(qǐng)求
response = session.post(url,headers=req_header,data=form_data)

7.3、token操作

import requests
url="http://xxxxxxxxxxxxxxx"

json={
    "head":{"accessToken":"","lastnotice":0,"msgid":""},
    "body":{"username":"15623720880","password":"48028d2558577c526a017883211b4066","forceLogin":0}
}
r=requests.post(url=url,json=json)
print(r.text)
print(r.cookies)

#登錄成功后返回token,帶入下一個(gè)接口
for i in range(0,1):
    try:
        url="xxxxxxxxxxxxxxxxxx"
        data={
            "head":{"lastnotice":0,"msgid":"","accessToken":"89a08bff-15d7-4d7a-9967-0b5f4fb699ce"},
            "body":{"clinicid":"978f661e-1782-43bd-8675-b0ff1138ab7c","deptid":"09b8515b-b01b-4771-9356-aed6b5aa01bf","doctorid":"65ac0251-10ff-473a-af8a-20e8969176f7","registtype":0,"card_num":"","bcc334":"","patientopt":1,"bkc368":"1","patient":{"cardid":"","medicalcardid":"","label":"","sourcetype":1,"nationid":"01","maritalstatus":0,"address":"","company":"","jobname":"","email":"","remark":"","bcc334":"","name":"11","gender":1,"phone":"","birthdate":"2020-03-23","patienttype":1,"szsbcardid":""}}
        }
        r=requests.post(url=url,json=data,timeout=0.09)
        print(r.text)
        print(r.cookies)
    except:
        print('error')

7.4、sign簽名

# appid: wxd930ea5d5a258f4f
# mch_id: 10000100
# device_info: 1000
# body: test
# nonce_str: ibuaiVcKdpRxkhJA
import hashlib
#需要加密的字符串
stringA="appid=wxd930ea5d5a258f4f&body=test&device_info=1000&mch_id=10000100&nonce_str=ibuaiVcKdpRxkhJA";
#構(gòu)建一個(gè)對(duì)象為md
md=hashlib.md5()
#對(duì)stringA字符串進(jìn)行編碼
md.update(stringA.encode()) 
#生成后的加密值
AES=md.hexdigest()
#把加密的結(jié)果,小寫(xiě)轉(zhuǎn)大寫(xiě) upper函數(shù)
AES=AES.upper()
print(AES)

參考微信支付:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=4_3

8、自動(dòng)化模塊劃分

  • config 配置文件(python package)#directory和python package大同小異
  • common 公共的方法(python package)
  • testdata 測(cè)試數(shù)據(jù)(python package)
  • test_case測(cè)試用例(python package)
  • report 報(bào)告(directory)
  • run_case 測(cè)試執(zhí)行(python package)
  • log 日志


8.1、config配置文件

def server_ip():
    '''
    ait_ip=''開(kāi)發(fā)環(huán)境的服務(wù)器ip
    sit_ip=''測(cè)試環(huán)境的服務(wù)器ip
    :return: 返回不同服務(wù)器的地址
    '''


    server_add={
        'dev_ip' : 'http://his.xxxxxxxxxxx.com',
        'sit_ip' : 'http://his.xxxxxxxxxxxx.comm'
    }
    return server_add['dev_ip']
------------------------------------------------------------------------------------
def sql_conf():
    '''
    host數(shù)據(jù)庫(kù)ip
    user數(shù)據(jù)庫(kù)用戶(hù)名
    password數(shù)據(jù)庫(kù)密碼
    database:連接數(shù)據(jù)庫(kù)名
    port數(shù)據(jù)庫(kù)端口
    chrset數(shù)據(jù)庫(kù)字符集 中文utf-8
    :return:
    '''
    host='localhost'
    user='root'
    password='123456'
    database='mysql'
    port=3306
    charset='utf8' #這用utf8,utf-8會(huì)報(bào)錯(cuò)
    return host,user,password,database,port,charset

8.2、common 公共的方法

# 封裝一個(gè)讀取Excel表格數(shù)據(jù)的函數(shù)
# 對(duì)Excel表格數(shù)據(jù)的讀取需要用到一個(gè)庫(kù)——xlrd庫(kù)
import xlrd
def get_excel_value(i):
    '''
    讀取表中一行的數(shù)據(jù)
    :return:返回2,3行數(shù)據(jù)
    '''
    filename = r"../testdata/jiekou.xls" #文件要用相對(duì)路徑
    book = xlrd.open_workbook(filename)  # 打開(kāi)一個(gè)工作薄,不需要手動(dòng)進(jìn)行關(guān)閉
    # sheet = book.sheet_by_name("Sheet1") 根據(jù)工作表的名字,獲取一個(gè)工作表對(duì)象
    sheet = book.sheet_by_index(0)  # 獲取一個(gè)工作表,以index的方式,這里是獲取第1個(gè)工作表
    return sheet.cell_value(i,1),sheet.cell_value(i,2)
# print(sheet.nrows) #打印所有行
# print(sheet.ncols) #打印所有列
# print(sheet.row_values(0))  #打印第一行
# print(sheet.col_values(0))  #打印第一列
# print(sheet.cell_value(0,1)) #打印第一行,第二列
# for i in range(1, sheet.nrows):

    # print(sheet.cell_value(i,1),sheet.cell_value(i,2))# 打印單元格[所有數(shù)據(jù)]的值
    # str='(sheet.cell_value(i,1),sheet.cell_value(i,2)))'
    # print(str)
# for i in range(1, sheet.nrows):
#     # for j in range(0, sheet.ncols):
#         print(sheet.cell_value(i,j)) # 打印單元格[i,j]的值
---------------------------------------------------------------------------------------------
import pymysql
from config.sql_conf import *
def get_sql(sql):
    '''
    :param sql:運(yùn)行查詢(xún)的sql語(yǔ)句
    :return:數(shù)據(jù)庫(kù)查詢(xún)結(jié)果
    '''
    #建立一個(gè)連接對(duì)象
    host, user, password, database, port, charset=sql_conf()
    db=pymysql.connect(host=host,user=user,password=password,database=database,port=port,charset=charset)
    #建立一個(gè)游標(biāo)
    cursor=db.cursor()
    #執(zhí)行sql語(yǔ)句
    cursor.execute(sql)
    #把sql運(yùn)行的數(shù)據(jù)保存在data變量里面
    data=cursor.fetchall() #獲取查詢(xún)出的所有的值
    cursor.close() #關(guān)閉游標(biāo)
    db.close() #關(guān)閉數(shù)據(jù)庫(kù)連接
    return data
# print(get_sql("SELECT help_topic_id FROM help_topic WHERE Name='MOD'")) #執(zhí)行sql語(yǔ)句
# print(type(get_sql("SELECT help_topic_id FROM help_topic WHERE Name='MOD'")))

8.3、testdata 測(cè)試數(shù)據(jù)

主要存放xls,txt,csv測(cè)試數(shù)據(jù)


8.4、test_case測(cè)試用例

from common.get_mysql import get_sql
from config.cof import server_ip
from common.get_excel import *
from config.sql_conf import *
import requests
# user_id=get_sql("SELECT help_topic_id FROM help_topic WHERE Name='MOD'")#提取數(shù)據(jù)庫(kù)數(shù)據(jù)
# print(user_id)#打印結(jié)果
# assert get_sql("SELECT help_topic_id FROM help_topic WHERE Name='MOD'")#斷言數(shù)據(jù)庫(kù)的數(shù)據(jù)是否存在

def test_aokao_login():
     url=server_ip()+'/service/user/login'
     username,password=get_excel_value(1) #讀取文件第二行數(shù)據(jù)
     json={
          "head":{"accessToken":"","lastnotice":0,"msgid":""},
          "body":{"username":username,"password":password,"forceLogin":0}
     }

     # usernamepassword=get_excel_value(4)[0] #讀取文件第二行數(shù)據(jù)
     # print(type(usernamepassword))
     # #把str類(lèi)型轉(zhuǎn)為字典格式 eval 函數(shù)
     # json=eval(usernamepassword)
     r=requests.post(url=url,json=json)
     print(r.text)
     assert r.status_code==200 #斷言狀態(tài)碼是否等于200
     assert '"accessToken":"89a08bff-15d7-4d7a-9967-0b5f4fb699ce",' in r.text #斷言返回信息是否包含accesstoken

def test_aokao_registadd():
     url = server_ip()+'/service/registration/registadd'
     data = {
          "head": {"lastnotice": 0, "msgid": "", "accessToken": "89a08bff-15d7-4d7a-9967-0b5f4fb699ce"},
          "body": {"clinicid": "978f661e-1782-43bd-8675-b0ff1138ab7c", "deptid": "09b8515b-b01b-4771-9356-aed6b5aa01bf",
                   "doctorid": "65ac0251-10ff-473a-af8a-20e8969176f7", "registtype": 0, "card_num": "", "bcc334": "",
                   "patientopt": 1, "bkc368": "1",
                   "patient": {"cardid": "", "medicalcardid": "", "label": "", "sourcetype": 1, "nationid": "01",
                               "maritalstatus": 0, "address": "", "company": "", "jobname": "", "email": "",
                               "remark": "", "bcc334": "", "name": "11", "gender": 1, "phone": "",
                               "birthdate": "2020-03-23", "patienttype": 1, "szsbcardid": ""}}
     }

     r = requests.post(url=url, json=data, timeout=0.09)
     print(r.text)
     print(r.cookies)
     assert r.status_code == 200  # 斷言狀態(tài)碼是否等于200

8.5、report 報(bào)告

主要存放html,xml報(bào)告


8.6、run_case 測(cè)試執(zhí)行

import pytest

'''
測(cè)試文件以test_開(kāi)頭,(以—_test結(jié)尾也可以)
測(cè)試類(lèi)以Test開(kāi)頭,并且不能帶有init 方法
測(cè)試函數(shù)以test_開(kāi)頭
斷言使用基本的assert即可
'''
#如何去運(yùn)行測(cè)試用例,_test開(kāi)頭的函數(shù)就可以,判斷用例運(yùn)行是否成功,assert斷言
if __name__=="__main__":
    #單個(gè)文件運(yùn)行,運(yùn)行添加,對(duì)應(yīng)的文件路徑,路徑要用相對(duì)路徑
    # pytest.main(['../test_case//test_case_01.py'])
    #多個(gè)文件運(yùn)行,運(yùn)行添加多個(gè)對(duì)應(yīng)的文件路徑,列表的形式,去添加多個(gè)文件的路徑
    # pytest.main(['../test_case/test_fore.py','../test_case/Dynamic correlation_token.py'])
    #運(yùn)行整個(gè)目錄,添加目錄的路徑
    pytest.main(['../test_case/','--html=../report/report.html','--junitxml=../report/report.xml'])

'''
pytest生成報(bào)告:
1、生成html報(bào)告
'--html=../report/report.html'
2、生成xml報(bào)告
'--junitxml=../report/report.xml'
'''

到此這篇關(guān)于python+requests+pytest接口自動(dòng)化的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)python 接口自動(dòng)化內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python基于keras訓(xùn)練實(shí)現(xiàn)微笑識(shí)別的示例詳解

    Python基于keras訓(xùn)練實(shí)現(xiàn)微笑識(shí)別的示例詳解

    Keras是一個(gè)由Python編寫(xiě)的開(kāi)源人工神經(jīng)網(wǎng)絡(luò)庫(kù),可用于深度學(xué)習(xí)模型的設(shè)計(jì)、調(diào)試、評(píng)估、應(yīng)用和可視化。本文將基于keras訓(xùn)練實(shí)現(xiàn)微笑識(shí)別效果,需要的可以參考一下
    2022-01-01
  • 淺談numpy中l(wèi)inspace的用法 (等差數(shù)列創(chuàng)建函數(shù))

    淺談numpy中l(wèi)inspace的用法 (等差數(shù)列創(chuàng)建函數(shù))

    下面小編就為大家?guī)?lái)一篇淺談numpy中l(wèi)inspace的用法 (等差數(shù)列創(chuàng)建函數(shù))。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-06-06
  • python 負(fù)數(shù)取模運(yùn)算實(shí)例

    python 負(fù)數(shù)取模運(yùn)算實(shí)例

    這篇文章主要介紹了python 負(fù)數(shù)取模運(yùn)算實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-06-06
  • 從源碼解析Python的Flask框架中request對(duì)象的用法

    從源碼解析Python的Flask框架中request對(duì)象的用法

    Flask中的request對(duì)象發(fā)送請(qǐng)求使用起來(lái)十分方便,但也有一些需要注意的地方,這里我們來(lái)從源碼解析Python的Flask框架中request對(duì)象的用法,需要的朋友可以參考下.
    2016-06-06
  • 在Python開(kāi)發(fā)環(huán)境中調(diào)用ChatGPT模型詳細(xì)過(guò)程

    在Python開(kāi)發(fā)環(huán)境中調(diào)用ChatGPT模型詳細(xì)過(guò)程

    在開(kāi)發(fā)過(guò)程當(dāng)中時(shí)常需要使用 ChatGPT 來(lái)完成一些任務(wù),但總是使用網(wǎng)頁(yè)交互模式去 Web 端訪(fǎng)問(wèn) ChatGPT 是很麻煩的,這時(shí)候我們可以使用代碼來(lái)調(diào)用 ChatGPT 模型,本文將詳細(xì)介紹在 Python 開(kāi)發(fā)環(huán)境中調(diào)用 ChatGPT 模型過(guò)程,,需要的朋友可以參考下
    2023-05-05
  • python線(xiàn)程中同步鎖詳解

    python線(xiàn)程中同步鎖詳解

    這篇文章主要為大家詳細(xì)介紹了python線(xiàn)程中同步鎖的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • 利用Python實(shí)現(xiàn)讀取Word文檔里的Excel附件

    利用Python實(shí)現(xiàn)讀取Word文檔里的Excel附件

    這篇文章主要為大家詳細(xì)介紹了如何利用Python實(shí)現(xiàn)讀取Word文檔里的Excel附件,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下
    2022-12-12
  • Selenium鼠標(biāo)與鍵盤(pán)事件常用操作方法示例

    Selenium鼠標(biāo)與鍵盤(pán)事件常用操作方法示例

    這篇文章主要介紹了Selenium鼠標(biāo)與鍵盤(pán)事件常用操作方法,結(jié)合實(shí)例形式分析了Selenium鼠標(biāo)事件與鍵盤(pán)事件常見(jiàn)方法與相關(guān)使用技巧,需要的朋友可以參考下
    2018-08-08
  • 舉例講解如何在Python編程中進(jìn)行迭代和遍歷

    舉例講解如何在Python編程中進(jìn)行迭代和遍歷

    這篇文章主要介紹了舉例講解如何在Python編程中進(jìn)行迭代和遍歷,是Python入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2016-01-01
  • Python中的 dir() 函數(shù)示例詳解

    Python中的 dir() 函數(shù)示例詳解

    dir()函數(shù)是Python 中一個(gè)非常有用的工具,可以用于查找對(duì)象的所有屬性和方法,如獲取當(dāng)前作用域的變量和方法、查找模塊中的導(dǎo)出內(nèi)容、動(dòng)態(tài)查找對(duì)象屬性等,通過(guò)本文的介紹和示例代碼,大家可以更全面地了解 dir() 函數(shù)的用法和注意事項(xiàng),需要的朋友參考下吧
    2022-03-03

最新評(píng)論