接口测试中,如何优雅的解决用例之间参数调用?

接口测试中,如何优雅的解决用例之间参数调用?

如何将上一用例的返回值的某个 key 的 value 值,当作下一用例的入参进行使用呢?

一种方式全局变量
def setup():
self.t=globals()

#登陆
def test_fxLogin(self):
url = “http://www.xxx.com/token
data = {“username”:“hffx”,“password”:“123456”,“platformType”:“NORMAL”}
headers = {“Content-Type”: “application/json”}
r = requests.post(url, data=json.dumps(data), headers=headers)
result = r.text
print(result)
jsr = json.loads(result)
fxtoken = jsr[‘data’]
self.t[‘fx’]=fxtoken
assert r.status_code==200

#下单
def test_buyprocedure(self):
url = “http://www.xxx.com/
data = {“data”}]}
headers = {“Authorization”:self.t[‘fx’],“Content-Type”: “application/json”}
r=requests.post(url,data=json.dumps(data),headers=headers)
result = r.text
print(result)
jsr=json.loads(result)
orderid=jsr[data][id]
self.t[‘orderids’]=orderid
assert r.status_code == 200

#支付
def test_payprocedure(self):
url = “http://www.xxx.com/”+self.t[‘orderids’]
data = {“data}]}
headers = {“Authorization”:self.t[‘fx’],“Content-Type”: “application/json”}
r=requests.post(url,data,headers)
print(r.text)
assert r.status_code==200

第二种方式
// 可以用setup_class

@classmethod
def setup_class(cls):
cls.pass_dic = {}

// 之后在测试用例里用self.pass_dic来存取结果

第三种方式
实例变量 self.xx
class TestWework:
def setup_class(self):
r = requests.get(
https://qyapi.weixin.qq.com/cgi-bin/gettoken,
params={
‘corpid’: ‘xxxxx’,
‘corpsecret’: ‘xxxxxxxx’
}
)
self.token = r.json()[‘access_token’]

def test_tags_list(self):
r = requests.post(
https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_corp_tag_list,
params={‘access_token’: self.token}
)
print(json.dumps(r.json(), indent=2, ensure_ascii=False))
assert r.status_code == 200
assert r.json()[‘errcode’] == 0