Python+Unittest接口自动化测试(一)---Request的基本使用&函数封装


一、Request的初体验

1. GET接口

import requests# http://fanyi.youdao.com/translate?&doctype=json&type=AUTO&i=今天   #网上找的,这个是有道词典的查询接口,传的参数也比较简单,可以直接使用
url = 'http://fanyi.youdao.com/translate'

data = {
        'doctype': 'json',
        'type': 'AUTO',
        'i': 'weather'
}

res = requests.get(url=url, params=data, timeout=5)   #get接口传参数时,关键字用params【post接口关键字用data,后面例子中可以见】

print(res.url)
print(res.json())

print(res.status_code)
print(res.raise_for_status())
print(res.encoding)
print(res.raw)print(res.request)
print(res.request.headers)

运行结果:

2. POST接口

import requests
url
= 'http://127.0.0.1:8000/login/' data = { 'username': 'Jessie', 'password': '123456' } res = requests.post(url=url, data=data, timeout=5) #post接口参数关键字,用data print(res.url) print(res.json())

运行结果:

二、封装函数发送GET/POST请求

把上面的发送POST/GET请求封装成函数,使用时直接调用,最后把数据返回出去

#coding:utf-8
import requests


def send_get(url, data):           #定义GET函数
    res = requests.get(url=url, params=data)
    result = res.json()
    return result

def send_post(url, data):          #定义POST函数
    res = requests.post(url=url, data=data)
    result = res.json()
    return result

def main(url, method, data):       #定义主函数。根据method是GET还是POST,调用相应的send_get()或send_post方法
    if method == 'POST':
        res = send_post(url, data)
    else:
        res = send_get(url, data)
    return res


url = 'http://127.0.0.1:8000/login/'
data = {
    'username':'Jessie',
    'password':'123456'
}

demo_test_post = main(url, 'POST', data)
print(demo_test_post)

运行结果:

三、封装类,发送GET/POST接口

#coding=utf-8

import requests
import json

class RunMain:
    def send_get(self, url, data):
        # res = requests.get(url=url, params=data).json()
        # return res

        res = requests.get(url=url, params=data)
        result = res.json()
        return json.dumps(result, indent=2, sort_keys=False, ensure_ascii=False)
        # dumps是将dict转化成str格式,loads是将str转化成dict格式
        # indent=2将输出结果缩进2个字符显示
        # sort_keys=False,输出结果是否按照关键字排序
        #son_dumps(dict)时,如果dict包含有汉字,json.dumps 序列化时对中文默认使用的ascii编码,想输出真正的中文需要指定ensure_ascii=False


    def send_post(self, url, data):
        # res = requests.post(url=url, data=data).json()
        # return res

        res = requests.post(url=url, data=data)
        result = res.json()
        return json.dumps(result, indent=2, sort_keys=False, ensure_ascii=False)


    def run_main(self, url, method, data=None):
        res = None
        if method == 'GET':
            res = self.send_get(url, data)
        else:
            res = self.send_post(url, data)
        return res


if __name__ == '__main__':
    # url = 'http://127.0.0.1:8000/login/'
    # data = {
    #     'username': 'Jessie',
    #     'password': '123456'
    # }

    url = 'http://fanyi.youdao.com/translate'
    data = {
        'doctype': 'json',
        'type': 'AUTO',
        'i': 'weather'
    }

run = RunMain()
# print(run.run_main(url, 'POST', data))
print(run.run_main(url, 'GET', data))

运行结果:

使用了json.dumps

 未使用json.dumps: