Selenium+Unittest+HTMLTestRunner框架更改为Selenium+Pytest+Allure(二)

1 代码框架

整体项目结构如图:

Common:公共库

Logs: 日志目录

Page: 页面元素

Report:测试报告

TestCase:测试用例

TestData: 测试数据

2 单模块运行

直接上代码:

python 复制代码
# -*- coding: utf-8 -*-
"""
-------------------------------------------------
   File Name:     TestJohnDeere.py
   Author :        曾良均
   QQ:             277099728
   Date:          12/1/2023 4:03 PM   
   Description :
-------------------------------------------------
   Change Activity:
                   
-------------------------------------------------
"""
__author__ = 'ljzeng'

import pytest
from Common.logger import Log
from Common.Operator import *
from Common.Logins import Logins
from Page.Credentials.CredentialsPage import CredentialsPage as cp
from selenium.webdriver.common.by import By
import allure

log = Log("TestJohnDeere")


class TestJohnDeere:
    driver = None
    lg = None
    page = None

    coll = (By.XPATH, '//*[@id="nav_arrow"]/div')

    @pytest.fixture()
    def begin(self):
        log.info('--------开始测试John Deere Credentials功能--------')
        self.driver = browser("chrome")
        self.lg = Logins()
        self.lg.login(self.driver, 'atcred@iicon004.com', 'Win.12345')
        self.driver.implicitly_wait(10)

        self.page = cp()
        ac = self.lg.get_attribute(self.coll, 'class')
        while True:
            if ac != 'icn collapse':
                ar = (By.ID, 'nav_arrow')
                self.page.click(ar)
                continue
            else:
                break
        self.lg.click(self.page.johndeere_menu)
        time.sleep(1)
        self.lg.switch_to_iframe(self.page.right_iframe)

        yield self.lg
        self.driver.quit()

    def add_jdlink(self, begin):
        log.info('点击 JD Link 的Add')
        if not begin.is_clickable(self.page.jdlink_add_btn):
            time.sleep(2)
        try:
            begin.click(self.page.jdlink_add_btn)
            time.sleep(1)
            self.driver.switch_to.window(self.driver.window_handles[1])
            time.sleep(2)
            txt = begin.get_text(self.page.jdlink_page_signin_lable)
        except Exception:
            log.info('Add 跳转失败!')
            return False
        else:
            log.info('Add 跳转成功!')
            self.driver.switch_to.window(self.driver.window_handles[0])
            if txt == 'Sign In':
                return True
            else:
                return False

    @allure.feature("测试Credentials功能")
    @allure.story("测试JD Link Credentials设置功能")
    def test_addJDlink(self, begin):
        """测试Add JD Link功能"""
        res = self.add_jdlink(begin)
        if res:
            log.info('Add JD Link 测试成功!')
        else:
            log.info('Add JD Link 测试失败!')
        assert res


if __name__ == '__main__':
    pytest.main(['-vs', 'TestJohnDeere.py'])  # 主函数模式

pytest.main('-vs', 'TestJohnDeere.py')

只运行当前文件,执行结果在Pycharm工作台显示。

3 主函数运行指定用例

3.1 pytest.ini定义要执行的用例文件、类、方法
python 复制代码
[pytest]
addopts=-vs                 
testpaths=./TestCase
python_files=Test*.py
python_classes=Test
python_functions=test
markers=
    smoke:
    uermanage:
3.2 主函数运行(run.py)
python 复制代码
# -*- coding: utf-8 -*-
"""
-------------------------------------------------
   File Name:     run.py
   Author :        曾良均
   QQ:             277099728
   Date:          12/4/2023 9:27 AM   
   Description :    这是所有测试用例运行的主函数
-------------------------------------------------
   Change Activity:

-------------------------------------------------
"""
__author__ = 'ljzeng'
import pytest
import os


if __name__ == '__main__':
    pytest.main(['-s', '-q', '--clean-alluredir', '--alluredir=allure-results'])
    os.system(r"copy environment.properties allure-results\\")
    os.system(r"allure generate -c -o Report\allure-report")
    # os.system(r"allure open Report\allure-report")  # 此命令会打开报告

4 查看Allure报告

通过主函数生成Allure报告文件,命令行打开报告:

python 复制代码
allure open Report\allure-report

浏览器自动打开报告:

5 遇到的问题

5.1 allure-results does not exists

当pytest.main中指定的--alluredir不是allure-results时,后面执行allure generate会把如题错误

解决方法:指定 --alluredir=allure-results 即可

相关推荐
测试界的小码农2 天前
【让 AI 生成的测试用例变成 pytest API 自动化脚本】
人工智能·测试用例·pytest
螺丝钉tina5 天前
[Playwright] [02]常用命令与调试:Debug UI、Mode和 Trace Viewer
开发语言·javascript·测试工具·ui·自动化
梧桐1686 天前
14天、31个缺陷、28个测试脚本—用 Claude Code 完成第三方软件测试
人工智能·测试工具
mewowaBc7 天前
postman使用细则
测试工具·postman·偏差
光影6277 天前
Unittest单元测试基础速记
测试工具·单元测试·json·测试覆盖率
xie881018028 天前
正则里的点号不匹配换行:多行模式的开关
运维·服务器·测试工具
2601_967987379 天前
Python在自动化测试中有哪些应用?
python自动化测试·selenium·appium·pytest·性能测试
东舟技术9 天前
从人工目检到AI视觉:智能座舱瞬态故障的自动化捕获与溯源
功能测试·测试工具·自动化
小猿A10 天前
【2026最新】Apifox下载、配置、实战教程(附官网安装包)
测试工具
泽众云测试10 天前
泽众软件与广州华南检验检测中心签约,PerformanceRunner性能测试工具助力第三方测评机构性能检测能力升级
软件测试·测试工具·性能测试