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

PyCharm UI运行/调试pytest提示fixture未找到,但终端运行正常

PyCharm运行pytest找不到fixture但终端正常的解决办法

问题描述

在PyCharm界面中运行或调试pytest测试时,出现以下错误:

def test_session_start_recording_post(self, user_token):
E       fixture 'user_token' not found

PyCharm列出的可用fixtures不包含user_token,但通过终端执行pytest时,测试可正常收集并运行,终端运行信息如下:

cachedir: .pytest_cache
metadata: {'Python': '3.11.0rc1', 'Platform': 'Linux-5.19.0-46-generic-x86_64-with-glibc2.35', 'Packages': {'pytest': '7.4.0', 'pluggy': '1.2.0'}, 'Plugins': {'Faker': '18.11.2', 'metadata': '3.0.0', 'html': '3.2.0', 'json-report': '1.5.0', 'excel': '1.5.0'}}
rootdir: <test_directory>
plugins: Faker-18.11.2, metadata-3.0.0, html-3.2.0, json-report-1.5.0, excel-1.5.0
collected 6 items

解决步骤

  • 核对PyCharm的pytest运行配置

    1. 打开Run/Debug Configurations,确认测试使用的Python解释器和终端执行pytest时的解释器一致(比如都是同一虚拟环境)
    2. 确认配置中的工作目录和终端运行时的<test_directory>完全相同
    3. 检查配置是否添加了异常的pytest参数,避免因参数导致fixture加载被阻断
  • 刷新PyCharm缓存
    点击顶部菜单栏File -> Invalidate Caches...,选择Invalidate and Restart,重启后让PyCharm重新扫描测试文件与fixture定义

  • 确认fixture定义位置
    确保user_token fixture定义在conftest.py文件中,且该文件位于测试根目录或当前测试文件所在目录下;同时检查fixture的作用域设置是否合理

  • 检查PyCharm的pytest插件状态
    打开Settings -> Plugins,确认pytest插件处于启用状态,若已禁用则启用后重启PyCharm

内容的提问来源于stack exchange,提问作者Sudip Neupane

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 16:33:17