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

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

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

获取运单号

录单

创建账单

确认账单

核销账单


相关文章
|
9月前
|
SQL 安全 Linux
Metasploit Pro 4.22.8-20251014 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.8-20251014 (Linux, Windows) - 专业渗透测试框架
451 1
Metasploit Pro 4.22.8-20251014 (Linux, Windows) - 专业渗透测试框架
|
9月前
|
Linux 网络安全 iOS开发
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
1041 1
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
|
10月前
|
安全 Linux 网络安全
Metasploit Pro 4.22.8-2025091701 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.8-2025091701 (Linux, Windows) - 专业渗透测试框架
523 2
Metasploit Pro 4.22.8-2025091701 (Linux, Windows) - 专业渗透测试框架
|
10月前
|
Linux 网络安全 iOS开发
Metasploit Framework 6.4.90 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.90 (macOS, Linux, Windows) - 开源渗透测试框架
591 1
Metasploit Framework 6.4.90 (macOS, Linux, Windows) - 开源渗透测试框架
|
数据可视化 前端开发 测试技术
接口测试新选择:Postman替代方案全解析
在软件开发中,接口测试工具至关重要。Postman长期占据主导地位,但随着国产工具的崛起,越来越多开发者转向更适合中国市场的替代方案——Apifox。它不仅支持中英文切换、完全免费不限人数,还具备强大的可视化操作、自动生成文档和API调试功能,极大简化了开发流程。
|
10月前
|
安全 Linux 网络安全
Metasploit Framework 6.4.88 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.88 (macOS, Linux, Windows) - 开源渗透测试框架
701 0
|
安全 Linux 网络安全
Metasploit Pro 4.22.7-2025061201 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.7-2025061201 (Linux, Windows) - 专业渗透测试框架
439 3
Metasploit Pro 4.22.7-2025061201 (Linux, Windows) - 专业渗透测试框架
|
10月前
|
缓存 安全 Linux
Metasploit Pro 4.22.8-2025082101 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.8-2025082101 (Linux, Windows) - 专业渗透测试框架
327 0
|
安全 Unix Linux
Metasploit Pro 4.22.7-2025052201 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.7-2025052201 (Linux, Windows) - 专业渗透测试框架
264 5
Metasploit Pro 4.22.7-2025052201 (Linux, Windows) - 专业渗透测试框架
|
数据采集 安全 Linux
Metasploit Pro 4.22.7-2025051201 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.7-2025051201 (Linux, Windows) - 专业渗透测试框架
272 4
Metasploit Pro 4.22.7-2025051201 (Linux, Windows) - 专业渗透测试框架

热门文章

最新文章