短信验证码接口测试案例

简单的验证码登录接口实例,用于初步窥探接口自动化测试,短信验证码与语音验证码能够成功,每一步都增加了必要的print信息,在控制台可以进行校验。
代码为实际某应用登录案例,实际情况以接口信息改变相应参数,主要注意通过post方法得到的信息需要转格式。

import requests
import random
def message(type):
    #获取验证码信息
#     global codeID  #声明全局变量
    prelist = ["130", "131", "132", "133", "134", "135", "136", "137", "138", "139",
               "147", "150", "151", "152", "153", "155", "156", "157", "158", "159",
               "186", "187", "188", "189"]
    phone_num0 = random.choice(prelist) + "".join(random.choice("0123456789") for i in range(8))
    phone = {
        "nationCode": "86",
        "phone": phone_num0
    }
    if type == 2:
        '''发送短信验证码'''
        re = requests.post("http://test-app.rolormd.com/Login/User/SendPhoneCode", data=phone)
        assert "success" in re.text
        print("短信验证码发送成功: " + phone_num0,"\n",re.json())
    elif type == 3:
        '''发送语音验证码'''
        re = requests.post("http://test-app.rolormd.com/Login/User/SendVoiceCode",data=phone)
        assert "success" in re.text
        print("语音验证码发送成功: " + phone_num0,"\n",re.json())
    elif type == 1:
        '''获取图形验证码'''
        re = requests.get("http://test-app.rolormd.com/Login/User/GetCodeImage")
        assert "success" in re.text
        print("获取成功: " + phone_num0,"\n",re.json())
    codeid = re.json()
    codeID = codeid["response"]["codeID"]
    return codeID
def check_message(codeID,code='000000',type='2'):
    #验证码校验
    message = {
        "codeID": codeID,
        "code": code,
        "type": type
    }
    print(message)
    re = requests.post("http://test-app.rolormd.com/Login/User/ValidateCode",data=message)
    print(re.text)
    if "success" in re.text:
        print("验证码正确")
    else:
        print("您输入的验证码有误,请重新输入")
if __name__=='__main__':
    codeID=message(3)
    check_message(codeID) 
  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值