美文网首页
基于python的最简单的接口测试demo

基于python的最简单的接口测试demo

作者: 青格er | 来源:发表于2018-04-26 16:00 被阅读71次

[toc]

1.请求参数

url :

http://test.xxxx/market/quote/get-last-quote-list

请求参数

data = {     "type": 2 }  

2.响应数据

{
    "code":200,
    "msg":"请求成功",
    "msgType":0,
    "errparam":"",
    "data":[
        {
            "symbol":"CL",
            "lastTradeDateOrContractMonth":"1805",
            "preClosePrice":"64.38",
            "lastPrice":"64.91",
            "time":"1522360214000"
        },
        {
            "symbol":"GC",
            "lastTradeDateOrContractMonth":"1804",
            "preClosePrice":"1324.2",
            "lastPrice":"1325.0",
            "time":"1522356660000"
        }        
    ]
}

3.源码

#/usr/bin/
# -*- coding: UTF-8 -*-
import requests  
import json  
  
def test_qualification_add():  
    url = "http://test. test.xxxx/market/quote/get-last-quote-list"    
    headers = {"Content-Type":"application/json"}    
    data = {                                      
        "type": 2
    }  
    r = requests.post(url = url,json = data,headers = headers)    
    #return r.json  
    # print (r.text)                                              
    print (r.status_code)   
    response_json = r.json()
    print "返回msg:",response_json.get("msg")
    print "返回msgType:",response_json.get("msgType")
    print "返回errparam:",response_json.get("errparam")
    List = response_json['data']
    print "返回的List",List
    for item in List:
        print "symbol===",item['symbol']
        print "lastTradeDateOrContractMonth===",item['lastTradeDateOrContractMonth']
        print "preClosePrice===",item['preClosePrice']
        print "lastPrice===",item['lastPrice']
        print "time===",item['time']

if __name__=="__main__":  
    test_qualification_add()  

4.知识点

  • requests.post(url = url,json = data,headers = headers)
    发送请求,得到的就是响应的数据
  • 将响应数据转化成json
    response_json = r.json()
  • 解析json中的字段
    value = response_json.get("keyname")

提示:

为保护公司机密代码中的接口是我随意写的哦,实际是不能使用的,小伙伴们如果想自己尝试,需要自己去找一个接口哦

备注

队友又在面向bug开发了,先去解决线上bug了,后续再陆续补充吧

相关文章

网友评论

      本文标题:基于python的最简单的接口测试demo

      本文链接:https://www.haomeiwen.com/subject/ahxmlftx.html