python3+requests interface automates the session operation method
- 2020-12-16 06:00:15
- OfStack
In the interface automation test, there are a lot of interfaces that operate based on the response value of the login interface. I tried many methods before, but failed. In fact, it is very simple to use session to do.
1. Create a global session in the login interface
# -*- coding: utf-8 -*-
import requests
''' Create in login module 1 A global session , in other interface operation when the login session And keep session the 1 consistency '''
s = requests.Session()# define 1 A global 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. Call the logged session on other interfaces and use this ES9en.post () to access other interfaces
from test_case.loggin import testlogin
import unittest
''' Here you import the previous login module and call the login module's session , and then execute the other interfaces '''
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()