flask集成allure测试报告,提供pytest执行测试用例接口和查看allure报告接口。
pytest+allure执行和收集测试报告可支持多方方式集成,和Jenkins、gitlab集成等。与Jenkins集成解决方案往上很多,不再赘述。和gitlab集成可参考https://github.com/allure-framework/allure2/issues/891。
之前做过与Jenkins集成,但是在查看allure报告的时候有时候会遇见某个测试case结果404的情况。今天将以与flask的集成说明下原因。
环境
gitlab-ci:做flask web服务器的build、deploy;
docker: 基础镜像管理;
docker-compose:docker容器的管理、编排;
flask:web服务器,用来提供测试接口和测试报告查看接口;
容器部署方面说下遇到的几个问题及解决方案
docker-compose传参,变量配置文件读取顺序:
Compose file
Shell environment variables
Environment file
Dockerfile
Variable is not defined
我是在gitlab-ci中这么传入变量的:
COMPOSE_PROJECT_NAME=${PROJECT_NAME} BRANCH=${BRANCH} docker-compose -f {docker-compose.yaml} --no-deps -d
giablab-ci中遇到的is docker running?错误
临时解决办法-重启docker服务:sudo service docker restart
解决办法二:
sudo service docker stop
sudo nohup dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock
下面说下有关查看测试报告问题
将allure报告文件放入templates文件下,利用flask的render_template直接去渲染对应的index.html页面。
现象:查看allure报告出现两个问题:
1、新一次的报告会“缓存”上一次的接口,需要多刷几次才能刷到最新结果;
2、查看具体case数据时会出现404的情况;
关于flask render_template渲染,当templates文件夹下文件变化后实时加载一般需要显示设置TEMPLATES_AUTO_RELOAD值。
@property
def templates_auto_reload(self):
"""Reload templates when they are changed. Used by
:meth:`create_jinja_environment`.
This attribute can be configured with :data:`TEMPLATES_AUTO_RELOAD`. If
not set, it will be enabled in debug mode.
.. versionadded:: 1.0
This property was added but the underlying config and behavior
already existed.
"""
rv = self.config["TEMPLATES_AUTO_RELOAD"]
return rv if rv is not None else self.debug
TEMPLATES_AUTO_RELOAD默认是None,但是在debug模式下为True。
因此设置app.config['TEMPLATES_AUTO_RELOAD'] = True
重启flask,发现还是存在1问题。
TEMPLATES_AUTO_RELOAD是解决本地开发,前端页面无法实时刷新问题的,一般在机器部署都是写好的template文件,因此此配置无效。
下面怀疑方向转向前端缓存,关于前端缓存,参考腾讯出品。
flask中可以这么解决,通过设置响应header使前端不缓存:
@app.after_request
def add_header(r):
"""
Add headers to both force latest IE rendering engine or Chrome Frame,
and also to cache the rendered page for 10 minutes.
"""
r.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
r.headers["Pragma"] = "no-cache"
r.headers["Expires"] = "0"
return r
这样就解决了1中问题,但是出现了个新的问题。两次pytest后,allure报告结果是两次运行的混合,且会出现2问题。
因此做了两种测试:
1、Google中隐身模式,执行两次会出现这个结果;
2、两台电脑访问也会出现这个问题;
那么可以确认问题出现在后端flask应用中。
flask是采用uwsgi部署的,关于Python代码变动后实现uwsgi自动刷新有如下几种解决方式:
Solution 1
One of the common approaches is to add following into uwsgi_conf.ini:
py-autoreload = 1
This will tell uWSGI that it needs to monitor file timestamps every second and reload the app once triggered.
Solution 2
Send graceful reload command to uWSGI Master FIFO:
Add following to your uwsgi_conf.ini:
master-fifo = /var/run/flask_uwsgi_fifo
And then reload uWSGI once you’re done with your Flask source file changes:
$ echo r > /var/run/flask_uwsgi_fifo
Solution 3
Similar to solution 2, but via touch-reload.
Add following to your uwsgi_conf.ini:
touch-reload = /var/run/flask_touch
And then reload your app via:
$ touch /var/run/flask_touch
Solution 4
Send SIGHUP to uWSGI pid file.
Add following to your uwsgi_conf.ini:
safe-pidfile = /tmp/flask.pid
And then reload your app via:
$ kill -HUP cat /tmp/flask.pid`
or
$ uwsgi --reload /tmp/flask.pid
尝试了方案2、方案3后发现还会出现2中问题,于是仔细分析了下uwsgi日志,发现200的请求都是同一个worker pid,404的请求是另外一个worker pid。因此推测allure获取数据的请求(.json请求),在另外一个pid上无render_templates渲染时的上下文和后端缓存(reload-on-rss)。
且allure官方做法allure open也是启动的单进程来访问。
因此将uwsgi.ini配置中进程数设置为1个processes = 1
,重启后再次访问allure报告,运转正常。