Python Selenium中conftest.py的driver fixture无法识别求助
问题解决:conftest.py中Fixture无法被识别的排查方案
可能原因及对应解决方法
pytest未在正确目录执行
确保你在conftest.py所在的主目录下运行测试命令。如果在子目录执行,需切换到主目录再运行:cd 主目录路径 pytest test.pyconftest.py缺失必要导入
检查conftest.py顶部是否导入了依赖模块,缺失会导致fixture加载失败:# 必须添加这些导入语句 import pytest from selenium import webdriver import timepytest缓存干扰加载
删除pytest缓存目录后重新运行测试:rm -rf .pytest_cache/ pytest test.py目录结构不符合pytest规范
确保测试文件(如test.py)和conftest.py在同一目录,或测试文件所在子目录能被pytest扫描到(默认扫描test_开头的文件/目录)。
验证conftest.py是否被加载
在conftest.py中添加打印语句,运行测试时查看是否输出,确认文件是否被识别:
import pytest from selenium import webdriver import time print("conftest.py已加载") @pytest.fixture def driver(): options = webdriver.ChromeOptions() driver = webdriver.Chrome(options=options) driver.maximize_window() time.sleep(3) yield driver driver.quit()
内容的提问来源于stack exchange,提问作者Iren
相关产品推荐
相关产品推荐

