日常记录一下,防止自己忘记~~~
环境
pycharm
java8+,jdk1.8
python == 3.7.4
pytest == 5.0.1
allure-pytest == 2.7.1
Allure ==2.12.1
下载安装包
(1)java8+,jdk1.8
安装allure,要先安装好jdk,运行java、javac这些命令都没有问题,要不安装allure时会报错,这个可以直接上官网安装。
(2)Allure
直接用Homebrew下载的allrue,Homebrew是macOS 缺失的软件包的管理器,要下载的话可以查看https://brew.sh/index_zh-cn
- 下载Homebrew
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
- 下载allure
安装完后,可以使用allure --version
查看安装的版本
$ brew install allure
(3)pytest
$ pip install pytest
(4)allure-pytest
$ pip install allure-pytest
注意:还有一个插件是pytest-allure-adaptor
,这个和allure-pytest
只需要安装一个,如果同时安装的话,运行的时候会出现错误。这两者的区别官网上给出的解释如下所示:
pytest-allure-adaptor
主要命令:
$ py.test --alluredir [path_to_report_dir] //生成报告
allure-pytest
这边使用的是allure-pytest,主要命令:
$ py.test --alluredir = %allure_result_folder% //生成报告
$ allure serve %allure_result_folder% //查看报告
运行
- 准备好测试代码,这代码是allure支持的一些pytest的特性,看看就好,摘自这里
https://docs.qameta.io/allure/#_python
import os
import pytest
@pytest.mark.xfail(condition=lambda: True, reason='this test is expecting failure')
def test_xfail_expected_failure():
"""this test is an xfail that will be marked as expected failure"""
assert False
@pytest.mark.xfail(condition=lambda: True, reason='this test is expecting failure')
def test_xfail_unexpected_pass():
"""this test is an xfail that will be marked as unexpected success"""
assert True
# 如果触发了条件 就跳过
@pytest.mark.skipif("2+2!=5",reason="this test is skipped by a triggered condition in @pytest.mark.skipif")
def test_skip_by_triggered_condition():
pass
if __name__ == "__main__":
pytest.main(['-s', 'test_case.py'])
- 打开pycharm的terminal,输入命令
这个命令会在当前目录下,运行所有符合pytest规则的测试用例,并在当前目录下,生成一个tests文件,报告生成在tests文件夹里
$ py.test --alluredir = %allure_result_folder% //生成报告
例:py.test --alluredir=./tests
- 查看allure
$ allure serve %allure_result_folder% //查看报告
例:allure serve tests
将tests文件下的测试文档,生成allure报告,输入命令后会出现一个网址,直接打开这个网址就行,会呈现allure的报告
直接打开http://10.8.1.158:61595,如果要退出的话,ctrl+c
- 还有一种查看报告的方法,生成测试报告的文件夹,打开网页查看。
该命令会生成一个allure-report文件夹,里面就是网页文档,找到里面的index.html,然后右击–>Open in Browser,就可以打开网页。
$ allure generate %allure_result_folder%
例:allure generate tests
如果要生成新的报告,则输入以下命令,然后重新打开网页即可。
$ allure generate %allure_result_folder% --clean
例:allure generate tests --clean
遇到的问题
-
pip._vendor.urllib3.exceptions.ReadTimeoutError:
HTTPSConnectionPool(host=‘files.pythonhosted.org’, port=443): Read timed out.
我先安装的pytest-allure-adaptor,总是安装不下来,可能是网速太慢
解决方式:
pip --default-timeout=100 install pytest-allure-adaptor
-
AttributeError: module ‘pytest’ has no attribute 'allure’
安装pytest-allure-adaptor后运行出现的问题,据说是pytest-allure-adaptor对python3不友好,所以要卸载pytest-allure-adaptor,再安装allure-pytest
$ pip uninstall pytest-allure-adaptor
$ pip allure-pytest
-
ValueError: option names {’–alluredir’} already added
同时安装了pytest-allure-adaptor和allure-pytest后,会报这个错误,所以需要卸载其中一个。
参考
[1]https://blog.youkuaiyun.com/Snow_python/article/details/90298896
[2]https://pypi.org/project/pytest-allure-adaptor/
[3]https://docs.qameta.io/allure/#_python
[4]https://www.jianshu.com/p/3378fa827924
[5]https://www.cnblogs.com/qq9261904/p/11139281.html