码迷,mamicode.com
首页 > 编程语言 > 详细

python接口测试实例完整代码

时间:2019-09-15 15:28:45      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:item   dig   cookies   text   sts   load   highlight   validate   xxxxxx   

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

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

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

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接口测试实例完整代码

标签:item   dig   cookies   text   sts   load   highlight   validate   xxxxxx   

原文地址:https://www.cnblogs.com/mtfan01/p/11522653.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!