python-requests数据驱动延伸

在 python-requests模块的讲解和应用 基础上进行数据驱动的延伸

 

task_01_requests.py

#-*- coding:utf-8 -*-
#task_01_requests.py

# 1:利用requests模块,编写一个可以完成http的get请求以及post请求的类。
# 2:利用登录和充值的两个数据,完成1中编写的类的单元测试(一条龙服务,包含测试报告)



import requests
class HttpRequests():
    def http_requests(self,url,params,http_mothed,cookies=None):
        if http_mothed=='get':
            res=requests.get(url,params,cookies=cookies)
            return res
        else:
            res=requests.post(url,params,cookies=cookies)
            return res

task_02_requests.py

#-*- coding:utf-8 -*-
#task_02_requests.py

#存放测试用例
import unittest
from study_181130_request.task_01_requests import HttpRequests

COOKIES=None

class TestHttpRequests(unittest.TestCase):

    # login_data={'mobilephone':18688773467,'pwd':'123456'}
    # login_data={'mobilephone':18688773467,'pwd':'1234567'}
    def setUp(self):
        # self.url='http://47.107.168.87:8080/futureloan/mvc/api/member/login'
        pass
    def __init__(self,url,param,http_method,excepted,methodName):#初始化传参;用初始化函数一定要记得去看看父类里面有没有初始化函数,如果有,需要超继承
        self.param=param#请求的数据
        self.http_method=http_method#请求方法
        self.url=url
        self.excepted=excepted
        super(TestHttpRequests,self).__init__(methodName)#超继承


    # def test_login(self):#登录成功 :手机号正确、密码正确;用例里面不可以传参数
    def test_api(self):#登录成功 :手机号正确、密码正确;用例里面不可以传参数
        global COOKIES#声明全局变量
        res=HttpRequests().http_requests(self.url,self.param,self.http_method,COOKIES)
        try:
            self.assertEqual(self.excepted,res.json()['msg'])
        except AssertionError as e:
            print('断言结果是:{}'.format(e))
            raise e

        if res.cookies:
            COOKIES=res.cookies#当res.cookies非空时,修改COOKIES的值

    # def test_error_pwd(self):#登录成功 :手机号正确、密码错误
    #
    #     res=HttpRequests().http_requests(self.url,self.param,self.http_method)
    #     try:
    #         self.assertEqual('用户名或密码错误',res.json()['msg'])
    #     except AssertionError as e:
    #         print('断言结果是:{}'.format(e))
    #         raise e
    #
    # def test_recharge(self):#充值成功
    #     # recharge='http://47.107.168.87:8080/futureloan/mvc/api/member/recharge'
    #     # recharge_data={'mobilephone':18688773467,'amount':'1000'}
    #     expected='充值成功'
    #
    #     res=HttpRequests().http_requests(recharge,recharge_data,'post',COOKIES)
    #     actual=res.json()['msg']
    #     self.assertEqual(expected,actual)



   # 数据驱动延伸--思路整理:
   #  先把可以参数化的login放到setUp()函数里,赋值给self.url
   #  再把请求数据login_data赋值给self.param和请求方法赋值给http_method放到初始化函数里;初始化函数不能直接重写,所以需要超继承
   #  参数化后要记得把函数里的参数改为参数化后的参数
   #  task_03_requests.py中创建实例的方法来添加用例;然后里面传参
   #  将login_data放到test_data列表中,然后利用for循环读取列表中的值,使suit.addTest()代码量减少
   #  然后再将放到test_data列表优化,加入http_method,列表里面嵌套字典
   #  添加充值的请求,此时url不能再放到setUp函数里面,可以放到test_data列表里面参数化
   #  test_api函数里面需要传入COOKIES参数,充值才能成功
   #  断言里面的期望值进行参数化

task_03_requests.py

#-*- coding:utf-8 -*-
#task_03_requests.py


import unittest
import HTMLTestRunnerNew
from study_181130_request.task_02_requests import TestHttpRequests
# login_data_01={'mobilephone':18688773467,'pwd':'123456'}
# login_data_02={'mobilephone':18688773467,'pwd':'1234567'}

# test_data=[{'mobilephone':18688773467,'pwd':'123456'},{'mobilephone':18688773467,'pwd':'1234567'}]
test_data=[{'param':{'mobilephone':18688773467,'pwd':'123456'},'http_method':'get','excepted':'登录成功',
            'url':'http://47.107.168.87:8080/futureloan/mvc/api/member/login'},

           {'param':{'mobilephone':18688773467,'pwd':'1234567'},'http_method':'post','excepted':'用户名或密码错误',
            'url':'http://47.107.168.87:8080/futureloan/mvc/api/member/login'},

            {'param':{'mobilephone':18688773467,'amount':'1000'},'http_method':'post','excepted':'充值成功',
             'url':'http://47.107.168.87:8080/futureloan/mvc/api/member/recharge'}]


suit=unittest.TestSuite()##装用例的地方
for item in test_data:
    suit.addTest(TestHttpRequests(item['url'],item['param'],item['http_method'],item['excepted'],'test_api'))#创建实例的方法来添加用例

    # suit.addTest(TestHttpRequests(item,'get','test_login'))#创建实例的方法来添加用例
# suit.addTest(TestHttpRequests(login_data_02,'post','test_login'))

# loader=unittest.TestLoader()
# suit.addTest(loader.loadTestsFromTestCase(TestHttpRequests))#通过loader从指定的测试类里面添加用例


#执行用例
with open('test_api.html','bw+') as file:
    Runner=HTMLTestRunnerNew.HTMLTestRunner(stream=file,verbosity=2,
                                            title='HTTP请求作业-单元测试报告',description='测试一下HTTP请求',tester='huimin'
                                            )
    Runner.run(suit)

测试报告

 

posted @ 2018-12-07 15:29  赵小丫  阅读(283)  评论(0编辑  收藏  举报