自动化测试===requests+unittest+postman的接口测试

news/2024/7/3 11:21:29
  • postman是一个跨平台的接口测试工具,下载链接在这里:https://www.getpostman.com/

 

  • unittest是一个单元测试框架,python中安装:pip install unittest

 

  • requests是一个发送http请求的库,安装:pip install requests

  官方文档:http://docs.python-requests.org/en/master/user/quickstart/,

  中文文档:http://docs.python-requests.org/zh_CN/latest/user/quickstart.html

 

以测试http://www.kuaidi100.com/query 为例:

有一个汇通快递的单号:350757819118

请求的url为http://www.kuaidi100.com/query?type=huitongkuaidi&postid=350757819118

用postman调试接口:

点击右上角的code,如下图:

以python--requests的格式复制:

复制得到的代码如下:

 
 
import requests

url = "http://www.kuaidi100.com/query"

querystring = {"type":"huitongkuaidi","postid":"350757819118"}

headers = {
    'cache-control': "no-cache",
    'postman-token': "9794775a-f3eb-0322-1365-01b775fa9925"
    }

response = requests.request("GET", url, headers=headers, params=querystring)

print(response.text)

新建py文件,命名为kuaidi.py,对复制的代码稍作调整:

 
 
import requests
import unittest

class KuaiDi(unittest.TestCase):
    def test_huitong_api(self):
        url = "http://www.kuaidi100.com/query"
        querystring = {"type":"huitongkuaidi","postid":"350757819118"}
        headers = {
        'cache-control': "no-cache",
        'postman-token': "9794775a-f3eb-0322-1365-01b775fa9925"
        }
        response = requests.request("GET", url, headers=headers, params=querystring).json()
        #print(response)
        self.assertEqual(response['status'],'200')    #断言
        self.assertEqual(response['message'],'ok')    #断言


if  __name__ == '__main__':
    unittest.main()

 

运行结果:

 

举例测试:

import requests
import json
import unittest
import time
from HTMLTestRunner import HTMLTestRunner  


class MyTest(unittest.TestCase):
    def setUp(self):
        print("[+]start")
    
    def tearDown(self):
        print("[+]end")
    
    def zhongtong(self,type = "zhongtong", id = "719857434111"):
        self.url = "http://www.kuaidi100.com/query"
        self.params = {
        "type":type,
        "postid":id
        }
        self.headers={'user-agent': 'my-app/0.0.1'}
        r = requests.get(url = self.url, params = self.params , headers = self.headers)
        print(r.status_code)
        return r

        

class ExpressInquiry(MyTest):
    def test001_type_valid(self):
        print("00001")
        zhongtong = self.zhongtong(type = "shentong")
        self.assertIn("快递公司参数异常",zhongtong.text)
    
    def test002_type_invalid(self):
        print("00002")
        zhongtong = self.zhongtong(type = "sssssssssssss")
        self.assertIn("参数错误",zhongtong.text)

    def test003_id_valid(self):
        print("00003")
        id = self.zhongtong(id = "719857434155")
        self.assertIn("交通工程学院菜鸟驿站",id.text)

    def test004_id_invalid(self):
        print("00004")
        id = self.zhongtong(id = "123")
        self.assertIn("参数错误",id.text)
    
    def test005_type_id_invalid(self):
        print("00005")
        type_and_id = self.zhongtong(type = "dads",id = "7777")
        #print(type_and_id.url)
        #print(type_and_id.text)
        self.assertIn("参数错误",type_and_id.text)

    def test006_type_id_null(self):
        print("00006")
        null = self.zhongtong(type = "", id = "")
        #print(null.url)
        #print(null.text)
        self.assertIn("参数错误",null.text)

def suite():
    now = time.strftime("%Y-%m-%d %H_%M_%S")
    filename = './' + now + 'test_result.html'
    fp = open(filename,'wb')
    runner = HTMLTestRunner(stream = fp,   
                                   title = "快递查询接口测试报告",  
                                   description = "测试用例执行情况:")


    suite = unittest.TestSuite()
    suite.addTest(ExpressInquiry("test001_type_valid"))
    suite.addTest(ExpressInquiry("test002_type_invalid"))
    suite.addTest(ExpressInquiry("test003_id_valid"))
    suite.addTest(ExpressInquiry("test004_id_invalid"))
    suite.addTest(ExpressInquiry("test005_type_id_invalid"))
    suite.addTest(ExpressInquiry("test006_type_id_null"))
    #unittest.TextTestRunner().run(suite)
    
    runner.run(suite)
    fp.close()
    


if __name__ == '__main__':
    #unittest.main(exit = False , verbosity = 2)
    #它是全局方法,把它屏蔽后,不在suite的用例就不会跑,exit = False表示中间有用例失败也继续执行;还有比较常用的verbosity=2,表示显示def名字
    
    suite()

 

 

 总结:

postman可以帮助完成一半的工作

unittest+requests可以实现断言,方便持续集成

 

 

转载于:https://www.cnblogs.com/botoo/p/8064458.html


http://www.niftyadmin.cn/n/2675417.html

相关文章

【AtCoder】【AGC013D】Piling Up

Description 你有一堆黑白球和一个盒子, 一开始,盒子里有n个不知道颜色的球, 接下来,你要对盒子进行m次操作: 1. 随机从盒子里取出一个球; 2. 往盒子里放一黑一白俩球; 3. 再随机从盒子里…

java 序列化时排除指定属性

java 序列化对象如何排除指定属性呢? java 中序列化对象有多种方式:struts2 ,jackson,json-lib (1)使用struts2 json插件 依赖的jar包:struts2-json-plugin-2.3.15.3.jar,xwork-core-2.3.15.3.jar,当然还有servlet-api.jar 范例: Java代码 private String getMessageJson(Pus…

ASP.NET的视图(Razor)循环产生html代码

需要要视图中Razor语法&#xff0c;循环产生一些html代码。产生后的html是这样的&#xff1a; <li data-transition"fade" data-slotamount"7" data-masterspeed"1500"><img src"~/Content/img/slider-images/XXX1111.jpg" a…

【AtCoder】【AGC009D】Uninity

Description 给出一棵树&#xff0c;要求确认一种点分治策略&#xff0c;使得点分树的深度最小。 Solution 显然&#xff0c;答案的上界为log&#xff08;然并卵&#xff09;。 我们先定义点权&#xff0c;一个点的点权为它在点分树上的深度&#xff0c; 显然&#xff0c;…

西游记人物

package com.hanqi;public class Xiyouji2 {String Name;double Height;String Weapon;void printName(){System.out.println(Name);}void printWeapon(){System.out.println("武器&#xff1a; "Weapon);}public static void main(String[] args){Xiyouji2 XiYouJiR…

Microsoft更新Cosmos DB,提供Cassandra支持,提高可用性保证

在上个月的Connect 2017大会上&#xff0c;Microsoft新发布了多个Azure Cosmos DB更新&#xff0c;其中包括支持使用Cassandra NoSQL数据库的API&#xff0c;以及更高的可用性保证。这样&#xff0c;用户可以在Cosmos DB内使用针对Cassandra NoSQL数据库的API去操作数据模型。此…

【AtCoder】【ARC064F】Rotated Palindromes

Description 求有多少个序列满足以下条件&#xff1a; 1. 序列有n位&#xff1b; 2. 序列的每位为1~m之间的整数&#xff1b; 3. 这个序列经过旋转以后可以变成一个回文串&#xff1b; Solution 这是一个悲惨的故事…..想了一天多&#xff0c;一直在想怎么减掉不合法的&…

网站常见的反爬虫和应对方法

这几天在爬一个网站&#xff0c;网站做了很多反爬虫工作&#xff0c;爬起来有些艰难&#xff0c;花了一些时间才绕过反爬虫。在这里把我写爬虫以来遇到的各种反爬虫策略和应对的方法总结一下。 从功能上来讲&#xff0c;爬虫一般分为数据采集&#xff0c;处理&#xff0c;储存三…