Pytest-如何将allure报告发布至公司内网

原理简介

使用Python 启动HTTP 服务器,指定一个端口号port ,内网用户可以使用ip+port访问报告。

本文章继续进阶,简单使用nginx进行一个代理,使用域名可以直接访问报告。

前情概述

Pytest-allure如何在测试完成后自动生成完整报告?-CSDN博客

代码一览

conftest.py

复制代码
# 报告使用的端口
port = 4965

def send_test_report():
    # 获取代码的根目录
    current_dir = os.path.dirname(os.path.abspath(__file__))
    shell_path = os.path.join(current_dir, "start_allure_server.sh")
    report_dir = os.path.join(current_dir, "report")
    results_dir = os.path.join(current_dir, "results")
    # 传参report_dir和results_dir,给shell脚本
    os.system(f'{shell_path} {report_dir} {results_dir} {port}')

在conftest文件中增加如上代码。

主要作用:找到报告的目录,并且带上port 参数一起传给下一步的shell 脚本,指定到最后一行时,则会去继续执行shell脚本里面的内容。

start_allure_server.sh

复制代码
#!/bin/bash

report_dir="$1"
results_dir="$2"
port="$3"

# 生成 Allure 报告
allure generate "$results_dir" -o "$report_dir" --clean
cd "$report_dir"

# 启动 HTTP 服务器
nohup python -m http.server "$port" &

这段代码意思很简单,获取前文传递的参数,并执行生成报告的命令。在这一步生成报告是为了防止还有用例没有执行完,和在conftest里面生成报告的效果一致。

初步效果展示

完成上述步骤后,至此就可以使用ip+port访问报告了。

在本地可以使用localhost+port访问。

使用nginx代理

下载nginx并安装

nginx: 下载

安装后解压即可使用。

配置

在安装目录的conf中,有一个nginx.conf

使用如下配置

复制代码
worker_processes 1;

# 错误日志配置
error_log logs/error.log info;

# PID 文件配置
pid logs/nginx.pid;

events {
    worker_connections 1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

	server {
		listen 80;
		server_name xxxxx; # 替换成你需要的域名

		# 临时重定向所有请求到/services/
		location / {
			return 302 /services/;
		}

		# 代理 /services/ 到 http://localhost:4965 
		location /services/ {
			proxy_pass http://localhost:4965/;
			proxy_set_header Host $host;
			proxy_set_header X-Real-IP $remote_addr;
			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
			proxy_set_header X-Forwarded-Proto $scheme;
		}
	}
}

简单来说,只需要把你使用的port替换,再指定你需要的域名,即可实现。

相关推荐
KYGALYX5 小时前
在Linux中备份msyql数据库和表的详细操作
linux·运维·数据库
余—笙5 小时前
Linux(docker)安装搭建CuteHttpFileServer/chfs文件共享服务器
linux·服务器·docker
lang201509285 小时前
Linux高效备份:tar与gzip完全指南
linux·运维·服务器
IDOlaoluo5 小时前
OceanBase all-in-one 4.2.0.0 安装教程(CentOS 7/EL7 一键部署详细步骤)
linux·centos·oceanbase
wanhengidc6 小时前
云手机的基本原理
运维·服务器·游戏·智能手机·云计算
篙芷6 小时前
两台服务器 NFS 共享目录实战
运维·服务器
catoop6 小时前
在 WSL 的 Ubuntu 中安装和配置 SSH 服务
linux·ubuntu·ssh
Hard but lovely7 小时前
linux: centos 软件包管理 yum源
linux·运维·centos
悲伤小伞7 小时前
Linux_Socket_UDP
linux·服务器·网络·c++·网络协议·udp
2301_816073837 小时前
Chrony服务器
运维·服务器