使用 pytest-xdist 分布式插件,保证 scope=session 的 fixture 在多进程运行情况下仍然能只运行一次

https://blog.csdn.net/qq_33801641/article/details/116220346

官方源码查看:https://www.cnblogs.com/canghai1024/p/13877314.html

return需要前置的函数(提前封装好)

# @pytest.fixture(scope="class", autouse=True)
# def mobile_captcha_fixture(tmp_path_factory, worker_id):
#     if worker_id == "master":
#         return get_mobile_captcha()
#
#     root_tmp_dir = tmp_path_factory.getbasetemp().parent
#     fn = root_tmp_dir / "data.json"
#     with FileLock(str(fn) + ".lock"):
#         if fn.is_file():
#             data = json.loads(fn.read_text())
#         else:
#             data = get_mobile_captcha()
#             fn.write_text(json.dumps(data))
#     return data
原文地址:https://www.cnblogs.com/erchun/p/15306044.html