项目地址
GIT:https://github.com/reportportal/reportportal
当我们进行接口自动化测试时,可以通过各种方法生成HTML结果,不过如果领导要看测试结果我们就要给领导发一份,同事要看测试结果我们就要给同事发一份,100看人想要看结果我们就要发给100个人,这样太麻烦了,那么有没有更简单的方法呢?当然有啦,我们使用ReportPortal就可以解决这个问题。
什么是ReportPortal?
ReportPortal 是一个统一的自动化测试报告收集、分析、可视化平台,可以集成多种测试框架,比如 TestNG、Selenium 等等。
它可以很轻松的与上图中的框架进行集成,能够实时展示测试结果,所有的自动化测试结果在一个地方统一查看;保留历史测试信息;能和 bug 跟踪系统集成,比如 Jira,我们使用了ReportPortal就可以集中管理测试结果啦,这样的话无论多少个人想要看我们的测试结果只要给他个url让他自己去看就好啦。
ReportPortal 服务器端包含以下服务:
service-authorization:授权服务。负责访问令牌的分发
service-api:API服务。应用后端
service-ui:UI服务。应用程序前端
service-index:索引服务。每个服务的信息和运行状况检查。
service-analyzer:分析仪服务。查找最相关的测试失败问题。
gatewayTraefik:网关服务。应用程序的主要入口点。网关使用的端口应打开并可以从外部网络访问。
rabbitmq:客户请求的负载均衡器。服务器之间的消息总线。
minio: 附件存储。
1、安装Docker(Engine,Compose)
2、下载dockercompose文件到你想要安装的文件夹
$ curl https://raw.githubusercontent.com/reportportal/reportportal/master/docker-compose.yml -o docker-compose.yml
3、在ReportPortal的文件夹执行docker-compose命令
4、启动
$ docker-compose -p reportportal up
要以守护程序模式启动ReportPortal,请添加’-d’参数:
$ docker-compose -p reportportal up -d
5、我们这时候可以在浏览器中打开我们部署的portal了
$ http://localhost:8080
使用用户名和密码进行访问:default\1q2w3e和 superadmin\erebus。
为了安全起见,请更改管理员密码。
进来是这个样子的
这时候我们的potal服务就已经启动了,但是我们的任务还没有结束,我们还要把自动化测试的结果推送给potal呢。
我们需要用到一个三方库client-Python:
GIT:https://github.com/reportportal/client-Python
那么这个库是做什么的呢,是用来实现reportpotal的监听的,通俗的讲就是向potal推送数据的。
1、我们先安装这个库
pip install reportportal-client
不过需要注意的是最新版本不支持低于5.0.0的Report Portal版本。
如果要装版本3.0就要执行:
pip install reportportal-client~=3.0
好啦,现在我们就可以向portal推送数据啦,给大家展示一个demo:
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 = “portal的url”
project = “portal的项目”
token = “这个是从portal中获取的,就是下图中的UUID”
launch_name = “Test launch”
launch_doc = “Testing logging with attachment.”
这样我们就可以把测试结果推送到portal啦!