在 selenium python 中运行 pytest 多进程时出现 pytest_sessionfinish 错误

问题描述 投票:0回答:0

我在conftest文件中有这段代码(使用pytest selenium运行自动化):

import os
from urllib import request

import pytest
from selenium.webdriver.chrome.options import Options
import allure
from selenium import webdriver


@pytest.fixture(autouse=True)
def setup(request):
    global driver
    options = Options()
    options.add_experimental_option("detach", True)
    options.add_argument("--headless")
    # options.add_argument("--no-sandbox")
    options.add_argument("--disable-dev-shm-usage")
    # options = webdriver.ChromeOptions()
    driver = webdriver.Chrome(options=options)
    request.cls.driver = driver
    driver.maximize_window()
    driver.get("https://magento.softwaretestingboard.com/")
    yield
    driver.quit()


def pytest_exception_interact(report):
    if report.failed:
        allure.attach(body=driver.get_screenshot_as_png(), name="screenshot",
                      attachment_type=allure.attachment_type.PNG)


def pytest_sessionfinish() -> None:
    environment_properties = {
     'browser': driver.name,
     'driver_version': driver.capabilities['browserVersion']
    }
    allure_env_path = os.path.join("allure-results", 'environment.properties')
    with open(allure_env_path, 'w') as f:
        data = '\n'.join([f'{variable}={value}' for variable, value in environment_properties.items()])
        f.write(data)

问题是当并行运行 pytest (pytest -n 5 --dist loadgroup --alluredir=allure-results --reruns 2 --reruns-delay 2) duw to def pytest_sessionfinish(该方法是将环境变量添加到 allure报告例如浏览器和浏览器版本)我在运行时遇到错误,这是错误:. [100%]回溯(最近一次调用最后一次): 文件“”,第 198 行,位于 run_module_as_main 文件“”,第 88 行,在 run_code 中 文件“C:\Users enik\PycharmProjects\pythonAutomationStore.venv\Scripts\pytest.exe_main.py”,第 7 行,位于 文件“C:\Users enik\PycharmProjects\pythonAutomationStore.venv\Lib\site-packages_pytes

python selenium-webdriver automation pytest allure
© www.soinside.com 2019 - 2024. All rights reserved.