使用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)