本篇文章将介绍如何使用开源的测试报告生成框架 Allure 生成规范、格式统一、美观的测试报告。
通过这篇文章的介绍,你将能够:
-
将 Allure 与 Pytest 测试框架相结合;
-
如何定制化测试报告内容
-
执行测试之后,生成 Allure 格式的测试报告。
1、Allure测试报告介绍
Allure 是一款非常轻量级并且非常灵活的开源测试报告生成框架。它支持绝大多数测试框架, 例如 TestNG、Pytest、JUint 等。它简单易用,易于集成。下面就 Pytest 如何与 Allure 集成做详细介绍。
2、Pytest 框架集成 Allure
Pytest 是 Python 的单元测试框架,非常方便和易用。强烈推荐对于用 Python 进行测试工作的小伙伴使用这个测试框架,相比与 Python 自带的 UnitTest 好用太多太多。今天我们主要是介绍如何将测试报告生成工具 Allure 集成到 Pytest 中。
2.1 安装 Allure Pytest Adaptor
Allure Pytest Adaptor 是 Pytest 的一个插件,通过它我们可以生成 Allure 所需要的用于生成测试报告的数据。安装 pytest-allure-adaptor 插件方法:
$ pip install pytest-allure-adaptor
2.2 使用Allure Pytest Adaptor改造基于Pytest的测试用例
pytest-allure-adaptor 官网中详细介绍了 pytest-allure-adaptor 所具有的功能。本篇文章不会再翻译一遍,而是从实际入手,给大家介绍如何将其应用到自己的框架中。
为了使用 Allure 生成报告,需要在 conftest.py 和测试脚本中加入 Allure 特性。
首先,conftest.py 中可以通过 allure.environment 方法将测试环境的信息输出到报告中,比如将测试时用的 host 和测试用的 browser 添加到测试报告中:
#!/usr/bin/env python
# coding=utf-8
import pytest
import allure
import yaml
@pytest.fixture(scope="session", autouse=True)
def env(request):
"""
Parse env config info
"""
root_dir = request.config.rootdir
config_path = '{0}/config/env_config.yml'.format(root_dir)
with open(config_path) as f:
&nbs