本篇文章是用python+pytest写了一个简单的接口自动化脚本,外加循环请求接口的语法,大家可以参考~
1 #实例一: 2 3 import requests 4 import pytest 5 import time 6 7 class TestSendRequest: 8 9 def test_request(request): 10 url = "" 11 data = {} 12 r = requests.get(url,params=data) 13 print(r.json()) 14 #断言 15 try: 16 assert r.json()['errno'] == 0 17 print("接口请求成功!") 18 except Exception: 19 print("接口请求失败!") 20 21 -------------------------------------------------------------------- 22 23 #实例二: 24 25 在接口的基础上进行for循环请求 26 27 def test_request(request): 28 #循环执行999次 29 for i in range(1): 30 url = "" 31 data = { 32 } 33 r = requests.get(url, params=data) 34 print("执行次数:"+str(i+1)) 35 time.sleep(0.1) 36 print(r.json()) 37 #断言 errno = 0 38 try: 39 assert r.json()['errno']==0 40 print("接口请求成功!") 41 except Exception : 42 print("接口请求失败!")