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

PyCharm无法识别Pytest测试用例但终端可正常识别的问题

PyCharm运行pytest找不到测试用例但终端正常的解决办法

问题场景

PyCharm设置为运行当前文件并点击「运行」按钮执行pytest时,无法识别测试用例;但在终端执行pytest命令却能正常找到并运行测试。此前PyCharm可正常工作,重启电脑后出现该问题。涉及测试文件为test_checkout_guest_user.py,代码如下:

import pdb
import pytest
from frameweerk.src.pages.homepage import homepage
from frameweerk.src.pages.Header import Header

@pytest.mark.usefixtures('init_driver')
class TestEndtoendchekoutguestuser:

    @pytest.mark.tcid33
    def test_end_to_end_checkout_guest_user(self):
     home_p= homepage(self.driver)
     header= Header(self.driver)

     home_p.go_to_home_page()
     home_p.click_first_add_to_cart_button()
     header.click_on_cart_header()

     pdb.set_trace()

解决方法

  • 核对PyCharm运行配置
    打开「Run/Debug Configurations」,确认当前配置的「Target」为「Script path」并指向该测试文件,或选择「Module name」填写正确模块路径;同时确保「Python interpreter」与终端使用的虚拟环境一致。
  • 重建项目索引
    点击菜单栏「File」→「Invalidate Caches...」,选择「Invalidate and Restart」,让PyCharm重新构建项目索引,修复重启后可能出现的索引异常。
  • 确认测试运行器设置
    进入「Settings」→「Tools」→「Python Integrated Tools」,检查「Default test runner」是否设置为「pytest」,若不是则切换后重启PyCharm。
  • 检查pytest插件状态
    打开「Settings」→「Plugins」,确认「pytest」插件处于启用状态,未启用则勾选后重启PyCharm。
  • 强制指定测试用例运行
    在运行配置的「Additional arguments」中添加test_checkout_guest_user.py::TestEndtoendchekoutguestuser::test_end_to_end_checkout_guest_user,直接指定要执行的测试用例路径。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 14:47:24