在進(jìn)行接口自動化測試時,有好多接口都基于登陸接口的響應(yīng)值來關(guān)聯(lián)進(jìn)行操作的,在次之前試了很多方法,都沒有成功,其實(shí)很簡單用session來做。
1、在登陸接口創(chuàng)建一個全局session
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
# -*- coding: utf-8 -*- import requests '''在登陸模塊創(chuàng)建一個全局session,在其他接口操作時帶入登陸時的session,保持session的一致性''' s = requests.Session() #定義一個全局session class testlogin(): login_url = "http://api-xxxxxx/api/Account/Login" username = "xxxxx" password = xxxxx def test_login( self ): data = { "UserName" : self .username, "Password" : self .password } r = s.post( self .login_url,data) print (r.cookies) return s |
2、在其他接口調(diào)用登陸的session,用這個session.post()去訪問其他接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
from test_case.loggin import testlogin import unittest '''這里導(dǎo)入之前的登陸模塊,調(diào)用登陸模塊的session,然后去執(zhí)行其他接口''' s = testlogin().test_login() class testtransfer(unittest.TestCase): def setUp( self ): self .transfer_url = "http://xxxxxxx/Transfer/DoTransferToGame" def test_transfer( self ): url = self .transfer_url data = { "Amount" :xx, "GamePlatform" : "xxxx" } r = s.post(url,data) print (r.text) if __name__ = = "__main__" : unittest.main() |
以上這篇python3+requests接口自動化session操作方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://www.cnblogs.com/jayson-0425/p/9760702.html