栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

基于fastapi实现6个接口(token拦截, 2个业务流程,接口参数依赖校验)已经通过postman测试,记录部署服务器和windows,用于pytest接口自动化框架的接口测试对象

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

基于fastapi实现6个接口(token拦截, 2个业务流程,接口参数依赖校验)已经通过postman测试,记录部署服务器和windows,用于pytest接口自动化框架的接口测试对象

基于fastapi实现6个接口
  • fastapi入门教程
  • 环境配置
    • liunx篇(腾讯云)
    • windows篇
  • 代码
  • 测试总结果
  • 接口请求参数和返回参数都正确的情况
    • 获取token
    • 获取运单号
    • 录单
    • 创建账单
    • 确认账单
    • 核销账单

fastapi入门教程

fastapi入门教程

环境配置 liunx篇(腾讯云)

先把代码文件丢进去,直接运行报错没有模块

pip install fastapi[all] 安装fastapi所有依赖


安装完成

再次执行

腾讯云设置防火墙,我之前用的8001

运行命令的端口号要改为host=‘0.0.0.0’

测试,注意修改域名为服务器的域名,端口为之前设置的端口

windows篇

pip install fastapi[all] 安装全部依赖就可以运行了,ip地址我写了方法自动获取,不过这个只能在局域网内访问

代码
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
@time    : 2022/6/21 
@Author  : LL
@File    : pytest_test_api.py
实现mock功能:
    token拦截
    获取token(需要token把写入yaml)
    2个业务流程
    接口参数依赖校验
下载相关依赖包
pip install fastapi[all]
'''

from fastapi import Depends, FastAPI, Header, HTTPException, status
from pydantic import BaseModel


def get_ip():
    '''获取本机ip地址'''
    import socket
    res = socket.gethostbyname(socket.gethostname())
    return res


def verify_token(token: str = Header(...)):
    if token != "em123dca666333":
        raise HTTPException(status_code=400, detail="Token 无效")


# 全局依赖
app = FastAPI(dependencies=[Depends(verify_token)])


@app.get("/get_token")
def get_token(token: str = Header(...)):
    '''获取token信息'''
    return {'token': token}

'''获取运单号'''
'''测试需要保存运单号'''
@app.get('/get_waybill_no')
def get_waybill_no():
    return {'waybill_no': 'lj520'}


class WaybillNo(BaseModel):
    waybill_no: str
    lu_dan_ren: str

'''录单'''
'''测试需要使用运单号'''
@app.post('/lu_dan', status_code=status.HTTP_201_CREATED)
def lu_dan(waybill: WaybillNo):
    if waybill.waybill_no != 'lj520':
        raise HTTPException(status_code=400, detail='运单号格式错误')
    return {'msg': '运单创建成功', 'waybill_info': waybill}


'''创建账单'''
'''测试需要保存账单号和创建人'''
class CreateBill(BaseModel):
    create_month: str
    create_name: str



@app.post('/create_bill')
def create_bill(bill: CreateBill):
    return {'bill_no': 'lj1314', 'bill_info': bill}


'''确认账单'''
'''测试需要使用账单号,需要保存确认人'''
class AffirmBill(BaseModel):
    affirm_name: str
    bill_no: str


@app.post('/affirm_bill')
def affirm_bill(bill: AffirmBill):
    if bill.bill_no != 'lj1314':
        raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail='账单不存在')
    return {'bill_no': bill.bill_no, 'bill_info': bill}


'''核销账单'''
'''测试需要使用账单号、创建人和确认人'''
class WriteOffBill(BaseModel):
    create_name: str
    affirm_name: str
    bill_no: str


@app.post('/write_off_bill')
def write_off_bill(bill: WriteOffBill):
    if bill.bill_no != 'lj1314':
        raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail='账单不存在')
    if bill.create_name != bill.affirm_name:
        raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail='创建人和确认人不一致')
    return {'bill_no': bill.bill_no, 'bill_info': bill}


if __name__ == '__main__':
    import uvicorn

    # app='test2:app'  文件位置:app
    uvicorn.run(app='pytest_test_api:app', host=get_ip(), port=8001, reload=True, debug=True)

测试总结果

接口请求参数和返回参数都正确的情况 获取token

获取运单号

录单

创建账单

确认账单

核销账单

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/982100.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号