如何使用ReportPortal测试结果?无论多少个人想要看?
优采云 发布时间: 2021-08-03 03:04如何使用ReportPortal测试结果?无论多少个人想要看?
项目地址
GIT:
我们在进行界面自动化测试的时候,可以通过各种方式生成HTML结果,但是如果leader想看测试结果,就得发一份给leader,如果同事想看测试结果,我们必须向同事发送一份副本。 100个人想看结果我们要发给100个人,这太麻烦了,有没有更简单的方法?当然,我们可以使用ReportPortal来解决这个问题。
什么是 ReportPortal?
ReportPortal 是一个统一的自动化测试报告采集、分析和可视化平台,可以集成多种测试框架,如TestNG、Selenium 等。
可以方便的与上图中的框架集成,可以实时显示测试结果。可以在一处查看所有自动化测试结果;保留历史测试信息;并且可以与bug跟踪系统集成,例如Jira,我们可以使用ReportPortal集中管理测试结果。这样,不管多少人想看我们的测试结果,只要给他一个网址让他自己看就行了。
仓库结构
ReportPortal 服务器端包括以下服务:
service-authorization:授权服务。负责访问令牌的分配
service-api:API 服务。应用后台
service-ui:UI 服务。应用前端
service-index:索引服务。每个服务的信息和健康检查。
service-analyzer:分析器服务。查找最相关的测试失败问题。
gatewayTraefik:网关服务。应用程序的主要入口点。网关使用的端口应该是开放的并且可以从外部网络访问。
rabbitmq:客户端请求的负载均衡器。服务器之间的消息总线。
minio:附件存储。
部署 ReportPortal
1、安装 Docker(引擎,撰写)
2、下载dockercompose文件到你要安装的文件夹
$ curl https://raw.githubusercontent.com/reportportal/reportportal/master/docker-compose.yml -o docker-compose.yml
3、在ReportPortal文件夹中执行docker-compose命令
4、Start
$ docker-compose -p reportportal up
要以守护进程模式启动 ReportPortal,请添加'-d'参数:
$ docker-compose -p reportportal up -d
5、这时候就可以在浏览器中打开我们部署的门户了
$:8080
使用用户名和密码访问:default\1q2w3e 和 superadmin\erebus。
为了安全,请更改管理员密码。
我是这样进来的
此时我们的potal服务已经启动了,但是我们的任务还没有结束,还要将自动化测试的结果推送到potal。
我们需要使用第三方库客户端-Python:
GIT:
那么这个库有什么作用呢?用于实现reportpotal的监控。通俗的说就是把数据推到了锅里。
1、我们先安装这个库
pip install reportportal-client
但是需要注意的是,最新版本不支持5.0.0以下的Report Portal版本。
如果要安装3.0版本,必须执行:
pip install reportportal-client~=3.0
好的,现在我们可以将数据推送到门户并向您展示一个演示:
import os
import subprocess
import traceback
from mimetypes import guess_type
from time import time
# Report Portal versions below 5.0.0:
from reportportal_client import ReportPortalServiceAsync
# Report Portal versions >= 5.0.0:
from reportportal_client import ReportPortalService
def timestamp():
return str(int(time() * 1000))
endpoint = "http://10.6.40.6:8080"
project = "default"
# You can get UUID from user profile page in the Report Portal.
token = "1adf271d-505f-44a8-ad71-0afbdf8c83bd"
launch_name = "Test launch"
launch_doc = "Testing logging with attachment."
def my_error_handler(exc_info):
"""
This callback function will be called by async service client when error occurs.
Return True if error is not critical and you want to continue work.
:param exc_info: result of sys.exc_info() -> (type, value, traceback)
:return:
"""
print("Error occurred: {}".format(exc_info[1]))
traceback.print_exception(*exc_info)
# Report Portal versions below 5.0.0:
service = ReportPortalServiceAsync(endpoint=endpoint, project=project,
token=token, error_handler=my_error_handler)
# Report Portal versions >= 5.0.0:
service = ReportPortalServiceAsync(endpoint=endpoint, project=project,
token=token)
# Start launch.
launch = service.start_launch(name=launch_name,
start_time=timestamp(),
description=launch_doc)
# Start test item Report Portal versions below 5.0.0:
test = service.start_test_item(name="Test Case",
description="First Test Case",
tags=["Image", "Smoke"],
start_time=timestamp(),
item_type="STEP",
parameters={"key1": "val1",
"key2": "val2"})
# Start test item Report Portal versions >= 5.0.0:
item_id = service.start_test_item(name="Test Case",
description="First Test Case",
start_time=timestamp(),
item_type="STEP",
parameters={"key1": "val1",
"key2": "val2"})
# Create text log message with INFO level.
service.log(time=timestamp(),
message="Hello World!",
level="INFO")
# Create log message with attached text output and WARN level.
service.log(time=timestamp(),
message="Too high memory usage!",
level="WARN",
attachment={
"name": "free_memory.txt",
"data": subprocess.check_output("free -h".split()),
"mime": "text/plain"
})
# Create log message with binary file, INFO level and custom mimetype.
image = "/tmp/image.png"
with open(image, "rb") as fh:
attachment = {
"name": os.path.basename(image),
"data": fh.read(),
"mime": guess_type(image)[0] or "application/octet-stream"
}
service.log(timestamp(), "Screen shot of issue.", "INFO", attachment)
# Create log message supplying only contents
service.log(
timestamp(),
"running processes",
"INFO",
attachment=subprocess.check_output("ps aux".split()))
# Finish test item Report Portal versions below 5.0.0.
service.finish_test_item(end_time=timestamp(), status="PASSED")
# Finish test item Report Portal versions >= 5.0.0.
service.finish_test_item(item_id=item_id, end_time=timestamp(), status="PASSED")
# Finish launch.
service.finish_launch(end_time=timestamp())
# Due to async nature of the service we need to call terminate() method which
# ensures all pending requests to server are processed.
# Failure to call terminate() may result in lost data.
service.terminate()
主要配置这些:
endpoint = "门户网址"
project = "门户项目"
token = "这个是从门户获取的,就是下图中的UUID"
launch_name = "测试启动"
launch_doc = "使用附件测试日志记录。"
这样我们就可以将测试结果推送到门户了!