本篇文章是用python+pytest写了一个简单的接口自动化脚本,外加循环请求接口的语法,大家可以参考~

实例一:

import requests
import pytest
import time
class TestSendRequest:
  def   test_request(request):
    url = ""
    data = {}
    r = requests.get(url,params=data)
    print(r.json())
#断言
    try:
          assert r.json()['errno'] == 0
          print("接口请求成功!")
    except Exception:
          print("接口请求失败!")

--------------------------------------------------------------------

实例二:

在接口的基础上进行for循环请求

 

def test_request(request):
#循环执行999次
            for i in range(1):
                url = ""
                data = {                
                    }
                r = requests.get(url, params=data)
                print("执行次数:"+str(i+1))
           time.sleep(0.1)
           print(r.json())
    #断言 errno = 0
           try:
                 assert r.json()['errno']==0
                 print("接口请求成功!")
           except Exception :
                 print("接口请求失败!")