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)


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM