首页 > 其他分享 >tep支持pytest-xdist分布式执行用例及合并Allure报告

tep支持pytest-xdist分布式执行用例及合并Allure报告

时间:2022-12-09 22:01:16浏览次数:64  
标签:allure xdist reports 用例 Allure pytest tep path config

tep近期更新频率较快,一方面是作者在积极投入到tep工具开发中;另一方面是我们聚集了20位小伙伴,一起合力打造EasyPytest测试平台,teprunner的FastAPI升级版本,依托于tep,帮你高效管理pytest测试用例。陆续也会有平台开发日志发布,欢迎持续关注。

预览内容:

1、pytest-xdist分布式执行用例,合并Allure报告;

2、global_vars全局变量配置;

预览项目:https://github.com/dongfanger/tep-template.git

分布式执行用例

借助于pytest-xdist,在命令行执行时添加参数-n auto

pytest -n auto

pytest-xdist会自动根据本地机器硬件配置,设置最优并发,并分发用例,分布式执行。

测试时先运行main.py,然后在case100目录下执行pytest -n auto --tep-reports

第一次串行,第二次xdist并行:

执行时间从50s一下降到5s,性能提升还是非常的明显。

合并Allure报告

pytest-xdist分布式执行,只要把allure源文件,也就是那一堆json文件,存到同一个目录下,报告的数据就是一体的,不需要单独合并。但是有个问题,tep封装了--tep-reports 命令行参数一键生成Allure报告,背后的逻辑是在pytest_sessionfinish hook函数里面实现的,分布式执行时,每个xdist的node都是一个单独的session,多个node就会生成多份报告:

c

10个node生成了11份报告,其中1份master节点生成的。pytest-xdist的原理如图所示:

master节点不运行任何测试,只是通过一小部分消息与节点通信。子节点执行后会通过workeroutput把数据回传给master节点。所以只需要通过是否有workeroutput属性来判断master节点:

def _is_master(config):
    """
    pytest-xdist分布式执行时,判断是主节点master还是子节点
    主节点没有workerinput属性
    """
    return not hasattr(config, 'workerinput')

然后只在主节点的pytest_sessionfinish,生成1次报告,就能避免生成多份报告。这样在xdist分布式执行模式下,--tep-reports也只会生成1份合并后的包含所有测试用例的Allure HTML报告。

完整实现代码:

import time
import shutil

import allure_commons
from allure_commons.logger import AllureFileLogger
from allure_pytest.listener import AllureListener
from allure_pytest.plugin import cleanup_factory

reports_path = os.path.join(Project.root_dir, "reports")
allure_source_dir_name = ".allure.source.temp"
allure_source_path = os.path.join(reports_path, allure_source_dir_name)


def _tep_reports(config):
    """
    --tep-reports命令行参数不能和allure命令行参数同时使用,否则可能出错
    """
    if config.getoption("--tep-reports") and not config.getoption("allure_report_dir"):
        return True
    return False


def _is_master(config):
    """
    pytest-xdist分布式执行时,判断是主节点master还是子节点
    主节点没有workerinput属性
    """
    return not hasattr(config, 'workerinput')


def pytest_addoption(parser):
    """
    allure测试报告 命令行参数
    """
    parser.addoption(
        "--tep-reports",
        action="store_const",
        const=True,
        help="Create tep allure HTML reports."
    )


def pytest_configure(config):
    """
    这段代码源自:https://github.com/allure-framework/allure-python/blob/master/allure-pytest/src/plugin.py
    目的是生成allure源文件,用于生成HTML报告
    """
    if _tep_reports(config):
        if os.path.exists(allure_source_path):
            shutil.rmtree(allure_source_path)
        test_listener = AllureListener(config)
        config.pluginmanager.register(test_listener)
        allure_commons.plugin_manager.register(test_listener)
        config.add_cleanup(cleanup_factory(test_listener))

        clean = config.option.clean_alluredir
        file_logger = AllureFileLogger(allure_source_path, clean)  # allure_source
        allure_commons.plugin_manager.register(file_logger)
        config.add_cleanup(cleanup_factory(file_logger))


def pytest_sessionfinish(session):
    """
    测试运行结束后生成allure报告
    """
    if _tep_reports(session.config):
        if _is_master(session.config):  # 只在master节点才生成报告
            # 最近一份报告的历史数据,填充allure趋势图
            if os.path.exists(reports_path):
                his_reports = os.listdir(reports_path)
                if allure_source_dir_name in his_reports:
                    his_reports.remove(allure_source_dir_name)
                if his_reports:
                    latest_report_history = os.path.join(reports_path, his_reports[-1], "history")
                    shutil.copytree(latest_report_history, os.path.join(allure_source_path, "history"))

            current_time = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime(time.time()))
            html_report_name = os.path.join(reports_path, "report-" + current_time)
            os.system(f"allure generate {allure_source_path} -o {html_report_name}  --clean")
            shutil.rmtree(allure_source_path)

global_vars全局变量

在使用环境变量模版时,有些变量需要在多个模版重复设置,tep新增了global_vars全局变量fixture,可以将重复设置的变量,定义在resources/global_vars.yaml

然后引用global_vars fixture取值即可:

def test(global_vars):
    print(global_vars["desc"])

实现代码:

@pytest.fixture(scope="session")
def global_vars():
    """全局变量,读取resources/global_vars.yaml,返回字典"""

    class Clazz(TepVars):
        def dict_(self):
            with open(os.path.join(Config.project_root_dir, "resources", "global_vars.yaml")) as f:
                return yaml.load(f.read(), Loader=yaml.FullLoader)

    return Clazz().dict_()

参考资料:

https://github.com/pytest-dev/pytest-xdist

如何从pytest-xdist节点获取数据 https://www.cnblogs.com/se7enjean/p/15924317.html

标签:allure,xdist,reports,用例,Allure,pytest,tep,path,config
From: https://www.cnblogs.com/df888/p/16961632.html

相关文章

  • allure环境搭建
    1、首先在官网下载allure:​​https://qameta.io/allure-report/​​2、下载解压后,配置环境变量3、在cmd命令行中查看allure是否配置成功4、python安装allure环境5、生成allu......
  • pytest中的pytest_addoption钩子函数-添加额外命令行,并且在用例中获取命令行传参
    pytest中的pytest_addoption钩子函数-添加额外命令行,并且在用例中获取命令行传参详细学习地址:https://blog.csdn.net/fx20211108/article/details/124719004在conftest.......
  • pytest + yaml 框架 -10.allure 生成报告
    前言本插件是基于pytest框架开发的,所以pytest的插件都能使用,生成报告可以用到allure报告pip安装插件pipinstallpytest-yaml-yoyoallure报告功能在v1.0.8版本......
  • 测试调试,ddt用例全部执行
    原理:通过inspect方法获取类的所有方法,再将过滤的ddt用例加入套件 foritemininspect.getmembers(CaseConvertAndUpdateModel,inspect.isfunction):if'test_Conv......
  • slam14(1) v4_1 卡尔曼滤波3 使用例子和代码 ardunio mpu6050
     代码https://github.com/TKJElectronics/KalmanFilter   原理剖析原理2卡尔曼融合滤波https://zhuanlan.zhihu.com/p/36374943 关键点1他的偏置和噪声......
  • Jenkins+Pytest+Allure
    环境准备Pytest负责Python代码测试Allure负责测试报告HTML界面展示Jenkins负责自动化测试安装完后,可以先来使用一下找一个使用Pytest的项目,直接去github......
  • 用例图
    一:用例图定义用来描述用户需求的图。需要强调功能,功能执行者,为执行者完成那些功能。二:用例图组成用例、参与者、参与者和用例之间的关系。三:用例的主要属性事件流描......
  • Selenium无浏览器页面执行测试用例—静默执行
    在执行WebUI自动化用例的时候,经常需要不打开浏览器执行自动化测试,这时就需要用到浏览器的静默执行。浏览器静默执行要点:1.定义Chrome的选项,两种方式任选chrome_options=......
  • pytest + yaml 框架 -7.用例分层机制
    前言当我们测试流程类的接口,需反复去调用同一个接口,就会想到复用API,在代码里面可以写成函数去调用。那么在yaml文件中,我们可以把单个API写到一个yaml文件,测试用例去调......
  • 自定义allure报告logo
    为了在测试报告中凸显公司的标识,可以自定义修改allure报告中,左上角部分的图片和文字。左上角内容由两部分组成,图片(左)和文字(右),均可修改。  一、上传图片路径:allure文......