请问有解决办法吗?

使用课程得测试框架,这里就不能使用上个接口返回得token还得重新调用上个方法,得到返回得token,就知道给下面第一个接口使用了

import mysql
import mysql.connector
#发送验证码
import requests

class TestMo:

_mobile=13762629090
_password="a123123"

def test_code(self):
    url="http://*****-h5.dev.hypercode.me/api/user/login/mobile/pwd/check/send"
    payload={
        "areaCode": "86",
        "mobile": self._mobile,
        "password": self._password,
        "useType": "LOGIN"
    }
    headers={
        "client":"android"
    }
    r=requests.post(url=url,json=payload,headers=headers)
    print(r.json())
 #   assert r.json()["data"] in '通知发送成功'
    print(r.text)

@classmethod
def code(self):
    config={
            "host":"*********",
            "port":"3306",
            "user":'admin',
            "password":"*********",
            "database":"****"
        }

    con=mysql.connector.connect(**config)
    cursor = con.cursor()
    sql="select * from captcha_send_record where send_mobile ={}  or send_email ={} order by create_time desc limit 1;".format(self._mobile,self._mobile)
    cursor.execute(sql)
    for one in cursor:
        Code=one[4]
        print(Code)
        return Code
    con.close()

@classmethod
def setup_token(self):
   # print("测试验证码", self.code())
    url = "http://*****-h5.dev.hypercode.me/api/user/login"
    payload = {
        "areaCode": "86",
        "captchaCode": self.code(),
        "clientType": "android",
        "mobile": self._mobile,
        "password": self._password
    }
    headers = {
        "client": "android"
    }
    y = requests.post(url=url, json=payload, headers=headers)
    print(y.text)
    token = y.json()["data"]["access_token"]
    print(token,"获取得token")
    return token


def test_Home(self):
   # print("token测试",self.setup_token())
    token=self.setup_token()
    url="http://*****-h5.dev.hypercode.me/api/content/coinPrice"
    headers={"Authorization":token,
             "client": "android"
             }
    m=requests.get(url=url,headers=headers)
    print(m.json())
    r=m.json()["data"]["list"][1]["symbol"]
    print(r)
    return token



def test_info(self):
    url="http://*****-h5.dev.hypercode.me/api/user/quereyUserInfo"
    headers={
        "Authorization": self.setup_token(),
        "client": "android"
    }
    y=requests.post(url=url,headers=headers)
    print(y.text)
  • 问题描述读不通,猜测是说因为拿token的方法写在了一个测试用例里导致别的用例拿不到?
  • 建议把这种需要传递的数据单独封装成一个方法,而不是将逻辑写在一个测试用例里
  • 这样不管是在setup步骤作为预备资源保存下来给别的用例进行使用,还是放到一个测试用例里去进行调用都很方便

意思是在加一个方法么?
把这些方法都封装一下了

对 封装一下,提高复用