前言
有這么一種情況,某一個接口在執行完之后需要等待一定時間才能生效,如果立馬執行下一個接口,就會發現會報錯
在python里面可以調用time函數,再設置用例之間的等待時間
在Httprunner里,也是可以實現用例等待的,debugtalk.py中定義等待時間的方法,再到用例中調用就可以了
操作實例
在debugtalk.py中寫入time.sleep()方法:
import time #設置用例等待時間 def sleep(response,n_secs): if response.status_code == 200: #接口請求code等於200 則等待n_secs 秒 time.sleep(n_secs) else: #接口請求code不等於200 則等待0.5 秒 time.sleep(0.5)
再到testcase用例中使用:
- config: name: 設置用例等待時間 variables: {} - test: name: 登錄接口-獲取token信息 request: headers: Content-Type: application/json method: POST url: http://localhost:80/login/ json: username: test password: a123456 extract: #extract 提取返回參數中的data值 - token: content.data validate: - eq: [status_code,200] teardown_hooks: - ${sleep($response,10)} #用例等待10s