美文网首页接口测试
Python 接口测试实践(Requests)-用例封装及测试报

Python 接口测试实践(Requests)-用例封装及测试报

作者: 落地逃 | 来源:发表于2020-05-25 16:56 被阅读0次

    作者:桃子

    今天以天气API接口为例,使用python语言实现用例编写、封装及报告生成功能

    API信息:

    天气API:https://www.sojson.com/blog/305.html

    URL:http://t.weather.sojson.com/api/weather/city/101030100

    请求方式:get

    参数:city 城市名称

    一  代码实现查询北京的天气信息

    步骤:

    1.新建 weather_api_test.py文件

    代码实现

    #-*-coding:GBK -*-

    import requests

    from pip._vendor.requests.models import Response

    url='http://t.weather.sojson.com/api/weather/city/101030100'

    r=requests.get(url)

    response_data=r.json()

    print(r.text)

    返回结果:

    二  用例集成到Unittest

    1.针对不同的参数场景进行测试

    2.设置断言判断执行结果是否符合预期

    实现原理:

    首先导入requests 库、unitest 、时间库

    其次,创建天气class类

    然后,分别创建4个函数,分别实现存放路径、正常传参、异常传参、缺省参数功能

    3.用例设计

    代码实现:

    新建 weather_api_unitest.py文件

    #-*-coding:GBK -*-

    import unittest

    import requests

    from time import sleep

    class weathertest(unittest.TestCase):

        def setUp(self):

            self.url='http://t.weather.sojson.com/api/weather/city/101030100'

            self.url_error='http://t.weather.sojson.com/api/weather/city/101030101'

            self.url_no='http://t.weather.sojson.com/api/weather/city'

        #参数正常

        def test_weather_tianjing(self):

            r=requests.get(self.url)

            result=r.json()

            self.assertEqual(result['status'],200)

            self.assertEqual(result['message'],'success感谢又拍云(upyun.com)提供CDN赞助')

            sleep(3)

        #参数异常

        def test_weather_param_error(self):

            r=requests.get(self.url_error)

            result=r.json()

            self.assertEqual(result['status'],400)

            self.assertEqual(result['message'],'获取失败')

            sleep(3)

        #参数缺省

        def test_weather_no_param(self):

            r=requests.get(self.url_no)

            result=r.json()

            self.assertEqual(result['status'],404)

            self.assertEqual(result['message'],'Request resource not found.')

            sleep(3)     

        if __name__=='_main_':

            unittest.main()

    三 测试报告生成

    1.创建文件夹如图,把测试用例放到test_case目录下

    2.下载BSTestRunner模块并放置到python Lib文件夹下

    如路径 C:\Python34\Lib

    3.创建run.py 文件

    代码:

    import unittest

    from BSTestRunner import BSTestRunner

    import time

    #指定测试用例和报告路径

    test_dir='./test_case'

    report_dir='./reports'

    #加载测试用例

    discover=unittest.defaultTestLoader.discover(test_dir, pattern='weather_api_unittest.py')

    #定义报告格式

    now=time.strftime('%Y-%m-%d %H_%M_%S')

    report_name=report_dir+'/'+now+'test_report.html'

    #运行用例并生成测试报告

    with open(report_name,'wb') as f:

        runner=BSTestRunner(stream=f,title="weather api test report",description="china city weather test report")

        runner.run(discover)

    4.运行run.py,在reports文件夹下查看生成的报告

    相关文章

      网友评论

        本文标题:Python 接口测试实践(Requests)-用例封装及测试报

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