【问题标题】:How to resolve "PytestDeprecationWarning: pytest.config global is deprecated如何解决“PytestDeprecationWarning: pytest.config global is deprecated
【发布时间】:2019-06-27 00:32:33
【问题描述】:

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

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

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

但我收到错误- UnboundLocalError:分配前引用了局部变量“驱动程序”

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

PytestDeprecationWarning:pytest.config 全局已弃用。请使用request.configpytest_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

【问题讨论】:

  • 你的主要问题不是警告(正如 q 标题所说),它只是一个贬损警告;这里的问题是 driver 的 UnboundLocalError 异常 - 这意味着您引用了一个未创建的变量。这意味着在不匹配任何选项的情况下感觉到 if/else 切换。检查 browser_info 的值 - 它是否与您正在检查的 3 个值中的任何一个匹配。
  • 我查过了。 driver = Chrome('./browser_exe/chromedriver.exe'),chrome驱动的相对路径。但错误仍然存​​在。
  • 嗯,仔细查看源代码,这对我来说是有问题的 - 您将 driverurl 设置为相同的值 - pytest.config.option.env。在您检查的夹具末尾是等于“本地”的值 - 打开您说您正在测试的站点;同时,您希望它的值是打开该浏览器的“chrome”、“Firefox”或“ie”之一。
  • 弃用警告绝对是个问题,尽管在​​这种情况下,这不是主要问题。要解决此警告(从而避免将来损坏),请参阅 this authoritative answer to a similar question

标签: python python-3.x selenium selenium-webdriver pytest


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2017-04-09
    • 1970-01-01
    • 1970-01-01
    • 2016-02-28
    • 2022-12-28
    • 1970-01-01
    • 2021-09-09
    • 2023-02-20
    • 2021-10-17
    相关资源
    最近更新 更多