【python自动化测试】获取cookie

使用框架unittest

处理cookie的3种方式

1、用setUp():
setUp()会在每个测试用例执行前执行,可以在这里写入需要获取cookie的登录操作

def setUp():
	res = requests.post(url,data)

def test_case():
	res_recharge = requests.post(url,data,cookies=res.json().cookies)

2、全局变量

COOKIE = None
class testCase(unittest.TestCase):
	def test_case(self):
		global COOKIE
		res = requests.post(url,data,COOKIE)
		if res.json().cookies:
			COOKIE = res.json().cookies

3、反射
python1.py

class GetCookie:
	COOKIE = None

python2.py

from python1 import GetCookie
class testCase(unittest.TestCase):
	def test_case(self):
		res = requests.post(url,data,gettaar(GetCookie,"COOKIE"))
		if res.json().cookies:
			COOKIE = settaar(GetCookie,"COOKIE",res.json().cookies)

你可能感兴趣的:(Python,自动化测试)