使用fastapi服务打开allure报告

使用fastapi服务打开allure报告

将allure 报告生成到固定路径,在使用fastapi作为服务,可以打开allure报告,当allure报告有更新时,只需要刷新网页即可

from fastapi import FastAPI, Request, HTTPException
from fastapi.staticfiles import StaticFiles
from fastapi.security import HTTPBasic, HTTPBasicCredentials
from starlette.status import HTTP_401_UNAUTHORIZED
import uvicorn
app = FastAPI(
 docs_url=None,  # 关闭swagger 
 redoc_url=None
)
security = HTTPBasic()


async def verify_username(request: Request) -> HTTPBasicCredentials:
    credentials = await security(request)
    print(f'credentials is {credentials}')
    if any([credentials.username != "admin", credentials.password != "admin"]):
        raise HTTPException(
            status_code=HTTP_401_UNAUTHORIZED,
            detail="Incorrect email or password",
            headers={"WWW-Authenticate": "Basic"},
        )
    return credentials.username


class AuthStaticFiles(StaticFiles):
    def __init__(self, *args, **kwargs) -> None:
        super().__init__(*args, **kwargs)

    async def __call__(self, scope, receive, send) -> None:
        assert scope["type"] == "http"

        request = Request(scope, receive)
        await verify_username(request)
        await super().__call__(scope, receive, send)


app.mount("/", AuthStaticFiles(directory='./report/html', html=True), name="allure_report")

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)

flask做法

from flask import Flask, send_file
app = Flask(__name__,static_url_path='')
@app.route('/report/<path:path>')
def send_report(path):
    return send_from_directory('report', path)
    # http://localhost:8080/report/html/index.html
if __name__ == '__main__':
    app.run(host='0.0.0.0',port=8080,debug=True)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值