• 自动化测试===unittest和requests接口测试案例,测试快递查询api(一)


    import requests
    import json
    import unittest
    
    
    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():
        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)
    
        
    
    
    if __name__ == '__main__':
        #unittest.main(exit = False , verbosity = 2)
        #它是全局方法,把它屏蔽后,不在suite的用例就不会跑,exit = False表示中间有用例失败也继续执行;还有比较常用的verbosity=2,表示显示def名字
        suite()             
  • 相关阅读:
    Hibernate框架简介
    [leecode]Evaluate Reverse Polish Notation
    linux 服务器之间配置免密登录
    大数据学习系列之一 ----- Hadoop环境搭建(单机)
    Hadoop hbase集群断电数据块被破坏无法启动
    CentOS 6 上安装 pip、setuptools
    CentOs6.7 python2.6升级到2.7.11
    安装phantomjs(Ubuntu版本 MacOS版本)
    Linux/Centos下安装部署phantomjs 及使用
    linux 查看系统磁盘、内存大小
  • 原文地址:https://www.cnblogs.com/botoo/p/7929091.html
Copyright © 2020-2023  润新知