一、基于 SSH 的远程执行方案
1. 环境准备流程
- 在目标服务器部署 HttpRunner 运行时环境:
# 远程服务器执行(需 Golang 1.18+ 和 Python 3.8+)
curl -ksSL https://httprunner.com/script/install.sh | bash
- 配置免密登录(本地机器执行):
ssh-copy-id user@remote_host
- 远程执行命令
# 进入项目目录,执行远程测试用例
ssh user@remote_host "cd /path/to/project && hrp run testcases/ --html-report"
- 结果文件同步
# 拉取测试报告
scp -r user@remote_host:/path/to/project/reports ./local_reports
二、CI/CD 流水线集成方案
GitLab CI 配置示例
stages:- testhttprunner_remote:image: python:3.9stage: testvariables:HRP_BIN_URL: "https://github.com/httprunner/httprunner/releases/download/v4.3.3/hrp-v4.3.3-linux-amd64.tar.gz"script:- apt-get update && apt-get install -y curl- curl -L $HRP_BIN_URL | tar xz -C /usr/local/bin- hrp run testcases/ --log-level debugartifacts:paths:- reports/
三、HTTP API 调用方案
1. 构建 API 服务层
# api_server.py
from fastapi import FastAPI
import subprocessapp = FastAPI()@app.post("/run-tests")
async def run_tests(config: dict):result = subprocess.run(["hrp", "run", config["test_path"]],capture_output=True,text=True)return {"status": result.returncode,"output": result.stdout,"error": result.stderr}
2. 远程调用示例
curl -X POST http://api-host:8000/run-tests \-H "Content-Type: application/json" \-d '{"test_path": "testcases/demo_api.yml"}'
四、分布式执行方案
1. 节点配置
# 启动工作节点(需开放 5557 端口)
hrp worker --host 0.0.0.0 --port 5557
2. 主控节点调用
# 分发测试任务
hrp run testcases/ --distributed \-w "remote_host1:5557,remote_host2:5557"
五、容器化部署方案
1. Dockerfile 构建
FROM python:3.9-slimRUN apt-get update && apt-get install -y curl
RUN curl -ksSL https://httprunner.com/script/install.sh | bashWORKDIR /app
COPY . .ENTRYPOINT ["hrp", "run", "testcases/"]
2. 远程执行命令
# 构建镜像
docker build -t httprunner-remote .# 运行容器
docker run -v $(pwd)/reports:/app/reports httprunner-remote
实现建议:
- SSH 方案适合临时执行场景
- CI/CD 方案适合自动化测试流水线
- 容器化方案保障环境一致性(需结合项目实际)
- 分布式方案适用于大规模性能测试(参考官方文档)