Pytest - xdist 保证多进程共享 session 级别fixture

背景:搜索自动化不同的测试文件件需要使用相同的变量

解决:

import logging

from tools import set_logging
import pytest
import time
from filelock import FileLock
import json
import os

set_logging.set_test_log()

@pytest.fixture(scope="session")
def get_batch_id(tmp_path_factory, worker_id):
    logging.info(f'worker_id:{worker_id}')
    logging.info(f'tmp_path_factory:{tmp_path_factory}')

    batch_id = str(int(time.time()))
    # 如果是单机运行 则运行这里的代码块【不可删除、修改】
    if worker_id == "master":
        """
        【自定义代码块】
        这里就写你要本身应该要做的操作,比如:登录请求、新增数据、清空数据库历史数据等等
        """
        # 如果测试用例有需要,可以返回对应的数据,比如 token
        return batch_id

    # 如果是分布式运行
    # 获取所有子节点共享的临时目录,无需修改【不可删除、修改】
    root_tmp_dir = tmp_path_factory.getbasetemp().parent
    logging.info(f'root_tmp_dir:{root_tmp_dir}')
    # 【不可删除、修改】
    fn = root_tmp_dir / "data.json"
    logging.info(f'fn:{fn}')
    # 【不可删除、修改】
    with FileLock(str(fn) + ".lock"):
        # 【不可删除、修改】
        if fn.is_file():
            # 缓存文件中读取数据,像登录操作的话就是 token 【不可删除、修改】
            batch_id = json.loads(fn.read_text())
            logging.info(f'缓存中读取batch_id:{batch_id}')
        else:
            """
            【自定义代码块】
            跟上面 if 的代码块一样就行
            """
            # 【不可删除、修改】
            fn.write_text(batch_id)
            logging.info(f'首次写入batch_id:{batch_id}')

        # 最好将后续需要保留的数据存在某个地方,比如这里是 os 的环境变量
        os.environ['batch_id'] = str(batch_id)
    return batch_id

产生的log文件:

2023-05-10 20:07:15,394 INFO [conftest.py(get_batch_id:17)] - worker_id:gw0
2023-05-10 20:07:15,394 INFO [conftest.py(get_batch_id:17)] - worker_id:gw1
2023-05-10 20:07:15,395 INFO [conftest.py(get_batch_id:18)] - tmp_path_factory:TempPathFactory(_given_basetemp=WindowsPath('C:/Users/win/AppData/Local/Temp/pytest-of-win/pytest-65/popen-gw0'), _trace=<pluggy._tracing.TagTracerSub object at 0x0000020CBA6DF9A0>, _basetemp=None)
2023-05-10 20:07:15,396 INFO [conftest.py(get_batch_id:18)] - tmp_path_factory:TempPathFactory(_given_basetemp=WindowsPath('C:/Users/win/AppData/Local/Temp/pytest-of-win/pytest-65/popen-gw1'), _trace=<pluggy._tracing.TagTracerSub object at 0x000002596E6CF970>, _basetemp=None)
2023-05-10 20:07:15,398 INFO [conftest.py(get_batch_id:33)] - root_tmp_dir:C:\Users\win\AppData\Local\Temp\pytest-of-win\pytest-65
2023-05-10 20:07:15,398 INFO [conftest.py(get_batch_id:33)] - root_tmp_dir:C:\Users\win\AppData\Local\Temp\pytest-of-win\pytest-65
2023-05-10 20:07:15,398 INFO [conftest.py(get_batch_id:36)] - fn:C:\Users\win\AppData\Local\Temp\pytest-of-win\pytest-65\data.json
2023-05-10 20:07:15,399 INFO [conftest.py(get_batch_id:36)] - fn:C:\Users\win\AppData\Local\Temp\pytest-of-win\pytest-65\data.json
2023-05-10 20:07:15,401 INFO [conftest.py(get_batch_id:51)] - 首次写入batch_id:1683720435
2023-05-10 20:07:15,402 INFO [test_search.py(test_search_keyword:313)] - get_batch_id:1683720435
 2023-05-10 20:07:15,456 INFO [conftest.py(get_batch_id:43)] - 缓存中读取batch_id:1683720435
2023-05-10 20:07:15,458 INFO [test_search_recommend.py(test_search_recommend_key_word:378)] - get_batch_id:1683720435

参考:
1.https://note.guoxiaorui.cn/pages/9bb196/
2.https://www.cnblogs.com/poloyy/p/14708825.html
3.https://blog.csdn.net/qq_37674086/article/details/122622699

posted @ 2023-05-10 20:25  chuangzhou  阅读(126)  评论(0编辑  收藏  举报