学会这些pytest-Allure常用特性allure.attach、allure.step、fixture、environment、categories,某里测开:大哥教教小弟

本文介绍了如何在Python的UI自动化测试中使用Allure.attach()和allure.attach.file()方法添加测试报告附件,包括文本和文件,并展示了如何配合pytest的参数化、fixture和测试步骤功能。同时,还讲解了如何配置环境信息和自定义缺陷分类。

allure.attach

allure.attach用于在测试报告中添加附件,补充测试结果。附件格式可以是txt、jpg等,附件内容通常是测试数据、截图等。

allure.attach提供了两种方法:allure.attach()allure.attach.file()

allure.attach()

作用:在测试报告中生成指定内容、名称、类型的附件

语法:allure.attach(body, name=None, attachment_type=None, extension=None)

参数说明:

  1. body,需要显示的内容,也可以理解为写入附件的内容
  2. name,附件名称
  3. attachment_type,附件类型,如csv、jpg、html 等,由allure.attachment_type提供
  4. extension:附件扩展名,不常用

allure.attach.file()

作用:向测试用例中上传附件

语法:allure.attach.file(source, name=None, attachment_type=None, extension=None)

参数说明:source为文件路径,其他参数与allure.attach()参数一致。

在UI自动化测试中,会经常用到这个方法来上传用例执行的截图。

示例

test_login.py

import allure
import pytest
import requests
import json

data = [("lilei", "123456"), ("hanmeimei", "888888"), ("xiaoming", "111111")]
ids = ["username:{}-password:{}".format(username, password) for username, password in data]

@allure.epic("xx在线购物平台接口测试")
@allure.feature("登录模块")
class TestLogin:

    @allure.story("用户登录")
    @allure.title("登录")
    @pytest.mark.parametrize("username, password", data, ids=ids)
    def test_login(self, username, password):
        headers = {"Content-Type": "application/json;charset=utf8"}
        url = "http://127.0.0.1:5000/login"
        _data = {
            "username": username,
            "password": password
        }
        allure.attach(
            body="用户名-{},密码-{}".format(username, password),
            name="登录参数",
            attachment_type=allure.attachment_type.TEXT
        )
        res = requests.post(url=url, headers=headers, json=_data).text
        res = json.loads(res)
        assert res['code'] == 1000
        
    @allure.story("用户退出登录")
    @allure.title("退出登录")
    def test_logout(self):
        '''这条测试用例仅仅只是为了举例说明allure.attach.file的使用'''
        print("退出登录,并截图")
        # 截图路径
        testcase_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
        source_path = testcase_path + "/screenshot/logout.jpg"
        allure.attach.file(
            source=source_path,
            name="退出登录后截图",
            attachment_type=allure.attachment_type.JPG
        )
        assert True

上述代码中使用了@pytest.mark.parametrize()

<think>好的,我现在需要帮用户搭建一个基于pytestAllure、Selenium、pytest-result-log和pytest-xdist的Web自动化试框架。首先,我得回忆一下这些工具的作用和整合方式。 用户提到的组件中,pytest试框架的核心,Selenium用于Web自动化,Allure生成报告,pytest-xdist用于并行试,pytest-result-log可能是指生成试结果日志的插件,不过通常pytest自己会生成日志,可能需要确认用户具体需求。不过根据引用[1]和[2],用户可能已经了解了一些安装步骤,比如安装allure-pytestpytest-xdist。 首先,应该从环境配置始。需要安装所有必要的库,比如pytest、selenium、allure-pytestpytest-xdist,可能还有pytest-result-log,不过这个插件我不太熟悉,可能需要查证是否存在,或者用户可能是指pytest-html或者其他日志插件。假设用户是指生成日志文件,可能需要配置pytest的日志功能,或者使用pytest-html生成HTML报告。不过根据引用[2]中的信息,用户提到了allure环境搭建,所以重点在Allure报告上。 接下来,项目结构方面,通常会有试用例目录、配置文件、页面对象模型、工具类(比如读取YAML配置文件,引用[3]中的yaml_load.py),以及试数据存放的位置。需要建议用户组织这些目录结构,比如创建test_cases、page_objects、utils、test_data等目录。 然后,编写基础用例。使用pytest和Selenium,每个试用例可能继承一个BaseTest类,该类处理浏览器的初始化和清理。例如,在conftest.py中定义fixture来管理浏览器的启动和关闭,这样每个试用例可以重复使用这个fixture。同时,需要整合Allure的步骤,添加注解如@allure.step来增强报告的可读性。 配置pytest.ini文件也很重要,可以设置pytest的默认参数,比如指定试目录、添加markers、配置日志格式和Allure的报告存储路径。例如,在pytest.ini中添加addopts参数,包括使用pytest-xdist的-n auto来并行执行,以及生成Allure报告所需的--alluredir参数。 试数据管理方面,引用[3]中提到了用YAML文件加载数据,所以可能需要建议用户将试数据存储在YAML文件中,并使用工具类读取,比如用户提供的yaml_load.py。 最后,执行试并生成报告,用户需要使用命令行运行pytest,并指定相关参数,然后使用Allure命令行工具生成HTML报告。可能还需要整合到持续集成系统中,或者配置IDE以便于运行。 在验证框架是否正常工作时,用户需要检查试是否并行执行,报告是否包含详细的步骤和截图,以及日志是否正确生成。可能还需要处理一些常见问题,比如浏览器的兼容性、依赖项的版本冲突,或者Allure报告的生成路径问题。 现在需要将这些步骤整理成一个清晰的指南,确保用户能够逐步操作。同时,根据用户提供的引用,确保引用的内容被适当参考,比如安装步骤和YAML读取的方法。最后,生成相关问题,帮助用户进一步深入学习和解决问题。</think>### 分步指南:搭建基于pytest+Allure+Selenium的Web自动化试框架 #### 一、环境准备 1. 安装基础包(Python 3.7+环境) ```bash pip3 install pytest selenium pytest-xdist pytest-html allure-pytest pytest-rerunfailures ``` 2. 安装Allure命令行工具(需提前配置Java环境) - 从[Allure官网](https://docs.qameta.io/allure/)下载压缩包并解压 - 将`bin`目录加入系统PATH环境变量[^2] #### 二、项目结构搭建 ``` project/ ├── conftest.py # pytest全局配置 ├── pytest.ini # 配置文件 ├── requirements.txt ├── page_objects/ # 页面对象模型 ├── test_cases/ # 试用例目录 ├── utils/ # 工具类 │ └── yaml_load.py # 数据加载工具[^3] └── reports/ # 试报告目录 ``` #### 三、核心配置实现 1. `conftest.py` 浏览器管理 ```python import pytest from selenium import webdriver import allure @pytest.fixture(scope="function") def browser(request): driver = webdriver.Chrome() driver.maximize_window() yield driver if request.node.rep_call.failed: allure.attach(driver.get_screenshot_as_png(), name="失败截图", attachment_type=allure.attachment_type.PNG) driver.quit() ``` 2. `pytest.ini` 配置文件 ```ini [pytest] addopts = -v -n auto --alluredir=./reports/allure-results --html=./reports/html-report.html markers = smoke: 冒烟试 regression: 回归试 ``` #### 四、试用例示例 ```python # test_login.py import allure from selenium.webdriver.common.by import By @allure.feature("登录模块") class TestLogin: @allure.story("成功登录场景") def test_valid_login(self, browser): browser.get("https://example.com/login") with allure.step("输入用户名"): browser.find_element(By.ID, "username").send_keys("admin") with allure.step("输入密码"): browser.find_element(By.ID, "password").send_keys("123456") with allure.step("点击登录按钮"): browser.find_element(By.CSS_SELECTOR, ".login-btn").click() assert "欢迎页" in browser.title ``` #### 五、执行与报告生成 1. 运行试(支持分布式执行) ```bash pytest test_cases/ -m "smoke" --reruns 2 ``` 2. 生成Allure报告 ```bash allure generate ./reports/allure-results -o ./reports/allure-report --clean allure open ./reports/allure-report ``` #### 六、关键技术整合 1. **pytest-xdist并行执行**:通过`-n auto`参数自动分配CPU核心数[^1] 2. **Allure增强报告**: - 使用`@allure`装饰器添加试步骤 - 自动附加失败截图 3. **Selenium集成**: - 使用Page Object模式管理元素定位 - 通过YAML文件管理试数据[^3] #### 七、框架验证 1. 检查是否生成两种报告: - HTML报告:`reports/html-report.html` - Allure交互式报告:`reports/allure-report` 2. 验证日志是否包含: - 执行时间戳 - 试步骤详情 - 错误堆栈信息
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值