Python-DDT实现接口自动化

Get请求参数化例子

import unittest
import requests
import ddt


@ddt.ddt
class MyTestCase(unittest.TestCase):
    @ddt.data(('qq_37616069', '80376776'), ('zhangchangbin123', '89310491'))
    @ddt.unpack
    def testGet(self, uid, pid):
        # Headers配置
        header = {
            "accept-encoding": "gzip, deflate, br",
            "accept-language": "zh-CN,zh;q=0.9",
            "cache-control": "max-age=0",
        }
        res = requests.get('https://blog.csdn.net/'+uid+'/article/details/'+pid, headers=header)
        status = res.status_code
        print(res.url)
        print(status)
        self.assertEqual(200, status)


if __name__ == '__main__':
    unittest.main()
    @ddt.data({'key1': 'value1', 'key2': 'value2'},
              {'key1': 'value3', 'key2': 'value4'})
    # @ddt.unpack
    def test_Get2(self, payload):
        # Headers配置
        header = {
            "accept-encoding": "gzip, deflate, br",
            "accept-language": "zh-CN,zh;q=0.9",
            "cache-control": "max-age=0",
         }
        r = requests.get("http://httpbin.org/get", headers=header,  params=payload)
        print(r.url)
        status = r.status_code
        print(status)

Post 请求

    @ddt.data({'key1': 'value1', 'key2': 'value2'},
              {'key1': 'value3', 'key2': 'value4'})
    def testPost(self, payload):
        header = {
            "accept-encoding": "gzip, deflate, br",
            "accept-language": "zh-CN,zh;q=0.9",
            "cache-control": "max-age=0",
         }
        url = 'http://httpbin.org/post'
        r = requests.post(url, headers=header, data=payload)
        print(r.text)
        status = r.status_code
        print(status)
        self.assertEqual(200, status)

转载于:https://www.cnblogs.com/lianstyle/p/11081127.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值