Ejemplo n.º 1
0
def test_recharge(info,login):
    """充值"""
    """先要替换"""
    if "#member_id#" in info['json']:
        info["json"] = info["json"].replace('#member_id#',str(login['id']))
    if "#wrong_member_id#" in info['json']:
        info["json"] = info["json"].replace('#wrong_member_id#', str(login['id'] + 1))

    # # token组装方式1:通过excel替换
    # if "#token#" in info['headers']:
    #     info["headers"] = info["headers"].replace("#token#",login['token'])

    # token 组装2:通过headers 添加,excel 表格里面不需要Authorization
    headers = json.loads(info["headers"])
    headers['Authorization'] = login['token']

    # 数据库访问,充值之前的余额
    db = DBHandler()
    sql = 'select leave_amount from member where id={}'.format(login['id'])
    result = db.query(sql)
    before_recharge_money =result['leave_amount']
    db.db_colse()

    data = json.loads(info['json'])
    res= requests.request(url= yaml_config['host'] + info['url'],
                     method=info['method'],
                     headers=headers,
                     json= data)
    res_body = res.json()
    print(res_body)
    try:
        assert res_body['code'] == info["expected"]
    except AssertionError as e:
        logger.error("用例失败:{}".format(e))
        raise e
    finally:
        excel = ExcelHandler(excel_file)
        excel.write('recharge',str(res_body),row=int(info['case_id']+1),column=9)
        if res_body['code'] == 0:
            db = DBHandler()
            sql = 'select leave_amount from member where id={}'.format(login['id'])
            result = db.query(sql)
            after_recharge_money = result['leave_amount']
            db.db_colse()
            money = Decimal(str(data['amount']))
            assert before_recharge_money + money == after_recharge_money
        if res_body['code'] == info["expected"]:
            excel.write('recharge',True,row=int(info['case_id']+1),column=8)
        else:
            excel.write('recharge',False,row=int(info['case_id']+1),column=8)
Ejemplo n.º 2
0
def generate_new_phone():
    """自动生成手机号"""
    fk = faker.Faker(locale='zh-CN')
    while True:
        phone = fk.phone_number()
        db = DBHandler()
        phone_in_db = db.query(
            'select mobile_phone from member where mobile_phone={}'.format(
                phone))
        # 查询数据库
        # 如果数据库里面有这条记录,重新生成新的手机号码,循环,不知道什么时候结束,用while
        db.db_colse()
        if not phone_in_db:
            return phone
    return phone