Skip to main content
 首页 » 编程设计

python之如何解决“PytestDeprecationWarning : pytest. config global is deprecated

2024年12月31日7lhb25

我正在测试一个名为- https://huew.co 的网站

为此,我正在开发我的框架。 在 utils/create_driver 中,我编写了我的代码,它将通过用户输入打开浏览器,就像用户在终端中输入“chrome”和 env=local

示例:python -m pytest test/test_infilect.py --type=chrome --env=local 然后 chrome 浏览器应该初始化并打开提供的 url。

但我收到错误- UnboundLocalError:赋值前引用了局部变量'driver'

和警告: PytestDeprecationWarning:pytest.config 全局已弃用。请使用 request.config 或 pytest_configure(如果你 e 一个 pytest 插件)代替。 url = pytest.config.option.env.lower()

PytestDeprecationWarning:pytest.config 全局已弃用。请使用 request.config 或 pytest_configure(如果你 e 一个 pytest 插件)代替。 browser_info = pytest.config.option.env.lower()

帮助解决同样的问题。

from selenium.webdriver import Chrome,Firefox,Ie 
import pytest 
 
# @pytest.fixture 
def get_browser_instance(): 
 
    browser_info = pytest.config.option.env.lower() 
 
    url = pytest.config.option.env.lower() 
 
    if browser_info == 'chrome': 
        driver = Chrome('./browser_exe/chromedriver.exe') 
 
    elif browser_info == 'firefox': 
        driver = Firefox('./browser_exe/geckodriver.exe') 
 
    elif browser_info == 'ie': 
        driver = Ie('./browser_exe/IEDriverServer.exe') 
 
    driver.maximize_window() 
    driver.implicitly_wait(60) 
 
    if url == 'local': 
        driver.get('https://huew.co/') 
 
    return driver 

当我从 pycharms 终端输入命令时,测试应该运行 - python -m pytest test/test_infilect.py --type=chrome --env=local

请您参考如下方法:

只需将 request 作为参数传递给您的 fixture,并将您的 fixture 主体中对 pytest.config 的引用替换为 request.config。