python接口测试实例完整代码

Posted mtfan01

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python接口测试实例完整代码相关的知识,希望对你有一定的参考价值。

  自动化测试其实是吧一个个测试点通过结构化设计串联起来,然后通过第三方模块也好,内置的模块也好,解决每一个测试点遇到的问题,以完成一个完整的自动化测试脚本。下面就完成一个自动化测试脚本。

  ............省略一万个字

  完成了各个功能(各个模块代码省略)处理的函数,最后将其拼接起来,就可以组成一个完整的自动化测试,完整代码如下:

import requests
import hashlib
from datetime import *
import json

username = ‘19999999999‘
password = hashlib.md5(b"123456").hexdigest()
url = ‘http://www.xxx.com/ajax/user_login/‘
form_data = "uernsme":username,"password":password
login_response = requests.post(url,data = form_data)
assert login_response.text == "success"
c = login_response

def make_oeder():
    global c
    url = ‘http://www.xxx.com/ajax/create_order/‘
    form_data = "restaurant_id":1198,"menu_items_total":"120.00",
                 "menu_items_data":"[‘id‘:15316,‘p‘:‘2‘,‘q‘:6]",
                 "delivery_fee":"3.00"
    make_reponse = requests.post(url,data = form_data,cookies = c)
    res = make_reponse.text
    id = json.loads(res)["order_id"]
    return id

def place_order(id):
    global c
    global username

    time = datetime.now()+timedelta(hours=1)
    url = ‘http://www.xxx.com/ajax/place_order/‘
    form_data = "order_id":id,"customer_name":"xxxx",
                 "mobile_number":username,"deliver_address":"xxxxxx",
                 "preorder":"yes","preorder_time":time,"pay_type":"cash"
    place_response = requests.post(url,data = form_data,cookies = c)
    res = place_response.text
    assert res == "success"
    print("订餐成功")

def sms():
    result = ask_sms()
    if result == "‘status‘:‘ok‘,‘need_sms‘:False":
        return
    else:
        request_sms()
        code = get_sms()
        validate_sms(code)


def ask_sms():
    global c
    global username
    url = ‘http://www.xxx.com/ajax/is_order_need/‘
    form_data = "mobile":username
    ask_reponse = requests.post(url,data = form_data,cookies = c)
    res = ask_reponse.text
    return res

def request_sms():
    global c
    global username
    url = ‘http://www.xxx.com/ajax/common_sms_need/‘
    form_data = "mobile":username
    sms_response = requests.post(url,data = form_data,cokies = c)
    res = sms_response.text
    assert res == "True"

def get_sms():
    global username
    url = ‘http://www.xxx.com/manager/login.action/‘
    form_data = "user":"admin","pwd":000000
    login_response = requests.post(url,data = form_data)
    cookies = login_response.cookies
    url2 = ‘http://www.xxx.com/manager/smsmanager/‘
    form_data2 = "phone":username
    code_response = requests.post(url2,data = form_data2,cookies = cookies)
    code = code_response.text
    assert code != ""
    return code

def validate_sms(code):
    global c
    global username
    url = ‘http://www.xxx.com/ajax/validate_sms_code/‘
    form = "mobile":username,"sms_code":code
    validate_response = requests.post(url,data = form,cookies = c)
    res = validate_response.text
    assert code == "True"

if __name__ == "__main__":
    id = make_oeder()
    sms()
    place_order(id)

几点注意事项如下:

1、函数定义没有先后顺序,只是执行有先后顺序。

2、断言即判断实际结果是否和预期结果一致,如果不一致,则中断整改程序的执行,因为后面的程序没有执行的必要,而且会打印出不一致的地方,以便可以快速定位到一步执行结果出错,方便查询问题。

3、函数之间的参数传递,需要将函数执行结果赋值给某一个参数,并在另外的函数之中定义形式参数,并最终将实际参数赋值到函数内的参数之中

以上是关于python接口测试实例完整代码的主要内容,如果未能解决你的问题,请参考以下文章

Python接口测试自动化说明及代码实例:含getpostputdelete等方法

接口测试-requests实例

基于Python的接口测试框架实例

[接口测试 -基础篇] 20 用flask写一个简单server用于接口测试

软件测试进阶之自动化测试——python接口测试实例

python---简单的接口测试实例