You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python Selenium中conftest.py的driver fixture无法识别求助

问题解决:conftest.py中Fixture无法被识别的排查方案

可能原因及对应解决方法

  • pytest未在正确目录执行
    确保你在conftest.py所在的主目录下运行测试命令。如果在子目录执行,需切换到主目录再运行:

    cd 主目录路径
    pytest test.py
    
  • conftest.py缺失必要导入
    检查conftest.py顶部是否导入了依赖模块,缺失会导致fixture加载失败:

    # 必须添加这些导入语句
    import pytest
    from selenium import webdriver
    import time
    
  • pytest缓存干扰加载
    删除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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 06:33:11