• 自动化测试中,cookie的调用方法。


    以cookie为例

    方法一:

    将返回的cookie写到setUp()中,每次执行用例之前就会调用一次。

    如:

    class AA(unittest.TestCase):
    def setUp(self): #生成cookie
    #登录
    self.login_url = "https://XXX"
    self.login_data = {XXXX}
    #cookie
    self.cookie=requests.post(self.login_url,self.login_data,verify=False).cookies
      def test_case_name(self):  #创建用例
      res=MM().ff("post",self.login_url,self.login_data,self.cookie) #调用self.cookie 
        def tearDown(self):
            pass
    
    

    方法二:

    全局变量:

    如:

    COOKIE=None

    class AA(unittest.TestCase):
      def test_login(self):  #产生cookie的用例
         global COOKIE #必须声明全局变量,才能修改
      res=MM().ff("post",self.login_url,self.login_data)

      if res.cookies:
           COOKIE=res.cookie
        ...
      def test_othercase(self):  #调用cookie的用例
         global COOKIE #必须声明全局变量,才能修改
      res=MM().ff("post",self.login_url,self.login_data,COOKIE)
    ...

    方法三:

    反射

    首先要写一个GetData类

    class GetData:

      COOKIE=None #存放cookie

    class AA(unittest.TestCase):

      def test_login(self):  #产生cookie的用例
      res=MM().ff("post",self.login_url,self.login_data)
      if res.cookies:
           setattr(GetData,'COOKIE',res.cookies)
        ...
      def test_othercase(self):  #调用cookie的用例
      res=MM().ff("post",self.login_url,self.login_data,getattr(GetData,'COOKIE'))

     

  • 相关阅读:
    axios+post获取并下载后台返回的二进制流
    vue+ckEditor5
    金额大写转换(改进版)
    vue+axios请求头封装
    移动端h5+vue失焦搜索,ios和android兼容问题
    vue滚动+滑动删除标记(移动端)仿qq/微信
    重置 centos 7 密码
    发现好玩的——github + git 有意思的用法
    github 中使用 issues
    java代理模式与装饰模式
  • 原文地址:https://www.cnblogs.com/guotang/p/12363504.html
Copyright © 2020-2023  润新知