Python 如何解决;PytestDeprecationWarning:pytest.config全局已弃用
我正在测试一个名为- 为此,我正在开发我的框架。 在utils/create_驱动程序中,我编写了我的代码,它将通过用户输入打开浏览器,就像用户在终端中输入'chrome',env=local一样 示例:python-m pytest/test_infict.py--type=chrome--env=local 然后chrome浏览器应该初始化并打开提供的url 但我犯了一个错误- 取消绑定LocalError:分配前引用的局部变量“driver” 和警告: PytestDeprecationWarning:pytest.config的Python 如何解决;PytestDeprecationWarning:pytest.config全局已弃用,python,python-3.x,selenium,selenium-webdriver,pytest,Python,Python 3.x,Selenium,Selenium Webdriver,Pytest,我正在测试一个名为- 为此,我正在开发我的框架。 在utils/create_驱动程序中,我编写了我的代码,它将通过用户输入打开浏览器,就像用户在终端中输入'chrome',env=local一样 示例:python-m pytest/test_infict.py--type=chrome--env=local 然后chrome浏览器应该初始化并打开提供的url 但我犯了一个错误- 取消绑定LocalError:分配前引用的局部变量“driver” 和警告: PytestDeprecationW
global已被弃用。请使用request.config
或pytest\u configure
(如果您需要
而不是一个pytest插件)。
url=pytest.config.option.env.lower()
PytestDeprecationWarning:pytest.config的global已被弃用。请使用request.config
或pytest\u configure
(如果您需要
而不是一个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_infict.py--type=chrome--env=local只需将request
作为参数传递给夹具,并将夹具主体中对pytest.config
的引用替换为request.config您的主要问题不是警告(正如q标题所说的),它只是一个去润滑警告;这里的问题是驱动程序的UnboundLocalError异常-这意味着您引用了一个未创建的变量。这意味着if/else开关在不匹配任何选项的情况下可以通过。检查browser\u info
的值-它是否与您正在检查的3个值中的任何一个匹配。我已检查。driver=Chrome('./browser_exe/chromedriver.exe'),Chrome驱动程序的相对路径。但是错误仍然在发生。嗯,仔细查看源代码,这对我来说是有问题的-您正在将driver
和url
设置为相同的值-pytest.config.option.env
。在你要检查的夹具的末尾,该值是否等于“local”-以打开你说要测试的站点;同时,你希望它的值是“chrome”、“Firefox”或“ie”中的一个来打开浏览器。不推荐警告绝对是一个问题,尽管无可否认不是本例中的主要问题。要解决此警告(从而避免将来损坏),请参阅。