距离上次更新已经过去 180 天了,文章内容可能已经过时。若内容或图片失效,请留言反馈(部分素材来自网络)。

记录配置 allure 的过程和简单的使用

检查是否有 java 的运行环境

  1. win+r 输入 cmd 回车,打开终端窗口

  1. 输入 Java 回车

  1. 如果没有显示就代表你的电脑没有安装 Java 运行环境,可以参考我的其中的一个教程,那里有关于 Java 的安装过程 JMeter 和 java8 安装

下载 allure

如果下载慢可以使用以下链接,不过它更新速度比较慢,不过也够用了。

配置 allure

  1. 把下载好的 allure 压缩包放到你需要解压的文件夹下。

  1. 进入到 allure 文件夹下,找到 bin 目录。

  1. 找到 allure.exe,cd 到此目录下运行 allure.exe 脚本。

  1. 运行结果如下

配置 allure 环境变量

  1. 复制 allurebin 目录路径,打开环境变量并找到 path 点击编辑

  1. 粘贴刚才复制的路径到 path

  1. 点击确定后检查是否配置成功,win+r 输入 cmd 回车运行终端,输入 allure 回车(也可以用 allure –version 检查版本号)

allure-pytest 搭建

  1. 安装 allure-pytestpytest

pytest 是组织和运行用例,并得到结果

  1. py 测试文件必须以 test_开头(或者以_test 结尾)
  2. 测试类必须以 Test 开头,并且不能有 init 方法
  3. 测试方法必须以 test_开头
  4. 断言必须使用 assert
python
1
2
3
pip install allure-pytest

pip install pytest
  1. 输入以下命令,执行 pytest 生成 allurejson 结果文件
python
1
2
3
pytest test_cals.py --alluredir ./report # ./report 也可以是文件夹的名称
或者
pytest.main(["--alluredir=文件夹的名称",""])

使用指令太过于麻烦,可以使用 pytest.ini 文件来运行(里面是不能出现中文的,注释可以删除)

ini
1
2
3
4
5
6
7
8
9
[pytest]
# 命令行参数
addopts = --alluredir ./temp -s
# 搜索文件名
python_files = class_datadriven.py
# 搜索的类名
python_classes = Test_*
# 搜索的函数名
python_function = test_*
注:如果使用报错是因为 `ini` 文件不支持 `UTF-8` 格式文件,这里需要更改为 `ANSI` 码来执行文件

这里用了 os 来执行指令,可以直接生成 html 文件

os.system('allure generate ./temp -o ./report --clean') 使用解释

参数解释
allure generate 命令,固定的
./temp 临时的 json 格式报告的路径
-o 输出 output
./report 生成的 allure 报告的路径
–clean 清空./report 路径原来的报告
  1. 查看测试报告:

两种方式:

方式一:

  • 输入 pytest test_py.py --alluredir ./temp(生成 json 文件,这里文件夹名称也可以是其它)

  • 测试完成后,查看实际报告,在线查看报告,会打开默认浏览器展示当前测试报告,在命令行输入:allure serve ./report 报告文件夹路径(生成 html)

方法二:

  • allure generate ./temp -o ./report –clean(指定生成报告的路径)

  • allure open -h 127.0.0.1 -p 8888 ./report(启动本地服务生成链接查看报告)

  1. 附加 allure 截图(在 allure report 中加入截图):

    python
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    from selenium.webdriver import Chrome
    import allure

    class BasePage:
    def __init__(self,driver:Chrome):
    self.driver = driver

    def attach_screenshot(self,name="截图"):
    file = self.driver.get_screenshot_as_png()
    allure.attach(file,name=name,attachment_type=allure.attachment_type.PNG)

allure 图标自定义

由于看这个图标不好看,需要更改,这里需要会一点 css 基础。

找到里面的 static 文件夹,里面有 csssvg 文件,更改你需要的图标和大小。

然后在 allure 根目录下找到 congfig 文件夹,找到里面的 allure.yaml 文件

在下面加入 custom-logo-plugin 代码