pytest生成测试用例,allure生成测试报告

测试代码

python 复制代码
import time
import pytest
import allure
from selenium import webdriver
from selenium.webdriver.edge.service import Service
from webdriver_manager.microsoft import EdgeChromiumDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


# edge的webdriver下载地址:
# https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/?form=MA13LH#downloads


@allure.epic("用户管理")
@allure.feature("用户登录")
class TestLogin:

    # 这两个方法是固定的写法,它们是pytest的测试固件(fixture)约定
    def setup_method(self):
        """测试前置条件"""
        service = Service(executable_path="C:/Users/Yoga7/my_allure/pythonProject/edgedriver_win64/msedgedriver.exe")
        self.driver = webdriver.Edge(service=service)
        # 窗口最大化
        self.driver.maximize_window()
        self.wait = WebDriverWait(self.driver, 5)
        # 打开测试网站
        self.driver.get("https://mayanan.cn/login.html")
        print("每个测试方法执行之前执行...")

    def teardown_method(self):
        """测试后置清理"""
        if self.driver:
            self.driver.quit()
        print("每个测试方法执行之后执行...")

    @allure.story("成功登录")
    @allure.title("验证用户使用正确用户名和密码能够成功登录")
    @allure.severity(allure.severity_level.CRITICAL)
    def test_successful_login_with_phone(self):
        print("测试方法开始执行了...")
        with allure.step("准备测试数据"):
            username = input("请输入用户名")
            # password = input("请输入密码")
            password = input("请输入密码:")
            allure.attach(f"用户名:{username}\n密码:{password}", name="测试数据")

        with allure.step("输入用户名"):
            username_input = self.driver.find_element(By.NAME, "username")
            username_input.clear()
            username_input.send_keys(username)
            allure.attach(f"输入用户名:{username}", name="操作日志")
            time.sleep(0.5)

        with allure.step("输入密码"):
            password_input = self.driver.find_element(By.ID, "password")
            password_input.clear()
            password_input.send_keys(password)
            allure.attach(f"请输入密码完成", name="操作日志")
            time.sleep(0.5)

        with allure.step("点击登录按钮"):
            login_btn = self.driver.find_element(By.XPATH, "//button[text()='登录']")
            login_btn.click()
            allure.attach("点击登录按钮完成", name="操作日志")
            time.sleep(0.5)

        with allure.step("点击确定按钮"):
            # 只判断是否存在,返回alert对象或者False,不执行切换
            self.wait.until(EC.alert_is_present())
            # 切换到alert弹窗
            alert = self.driver.switch_to.alert
            alert.accept()
            time.sleep(5)

        with allure.step("验证登录成功"):
            try:
                h1 = self.wait.until(
                    EC.presence_of_element_located((By.XPATH, "//h1[text()='DeepSeek AI大模型搜索']"))
                )
                assert h1.is_displayed()
                login_success = True
                allure.attach("登录成功", name="验证结果")
            except Exception as err:
                login_success = False
                allure.attach("登录失败", name="验证结果")

        # 添加截图
        allure.attach(self.driver.get_screenshot_as_png(),
                      name="登录结果页面",
                      attachment_type=allure.attachment_type.PNG)
        assert login_success, "未检测到成功登录的标识"

1. pytest生成测试用例

pytest .\test_login02.py --alluredir=allure_results -v -s

2. allure生成测试报告

allure serve allure_results

3. 生成静态报告

allure generate .\allure_results -o .\allure_report --clean

相关推荐
卓码软件测评6 小时前
第三方软件测试机构:【“Bug预防”比“Bug发现”更有价值:如何建立缺陷根因分析与流转机制?】
功能测试·测试工具·单元测试·测试用例·压力测试·可用性测试
南汐以墨6 小时前
BUG与测试用例
测试用例·bug
测试老哥10 小时前
python+requests+excel 接口测试
自动化测试·软件测试·python·测试工具·测试用例·excel·接口测试
天才测试猿16 小时前
Selenium三大等待详解
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
程序员小远19 小时前
Postman接口测试: Postman环境变量&全局变量设置,多接口顺序执行详解
自动化测试·软件测试·python·测试工具·测试用例·接口测试·postman
天才测试猿19 小时前
Postman使用方法
自动化测试·软件测试·测试工具·职场和发展·测试用例·接口测试·postman
程序员三藏19 小时前
Postman定义公共函数
自动化测试·软件测试·python·测试工具·测试用例·接口测试·postman
测试19981 天前
如何写出一个完整的测试用例?
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·接口测试
tealcwu2 天前
【Unity踩坑】Unity测试用例命名空间错误解决方案
unity·游戏引擎·测试用例
我的xiaodoujiao2 天前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 22--数据驱动--参数化处理 Json 文件
python·学习·测试工具·pytest