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

显式指定测试文件时pytest无法找到测试用例的问题排查

pytest运行指定测试文件报错问题

项目结构

(pytest) bob@Roberts-Mac-mini ds % tree
.
├── ds
│   └── __init__.py
└── tests
    ├── __pycache__
    │   ├── test_compare.cpython-311-pytest-7.2.0.pyc
    │   ├── test_square.cpython-311-pytest-7.2.0.pyc
    │   └── test_stack.cpython-311-pytest-7.2.0.pyc
    ├── test_compare.py
    └── test_square.py

4 directories, 6 files

问题现象

  1. 项目根目录直接运行pytest,可正常收集并运行测试用例,输出如下:
===================================================================================== test session starts =====================================================================================
platform darwin -- Python 3.11.0, pytest-7.2.0, pluggy-1.0.0
rootdir: /Users/bob/Documents/work/pytest_tut
collected 6 items                                                                                                                                                                             

ds/tests/test_compare.py F..                                                                                                                                                            [ 50%]
ds/tests/test_square.py ..F                                                                                                                                                             [100%]

========================================================================================== FAILURES ===========================================================================================
________________________________________________________________________________________ test_greater _________________________________________________________________________________________

    def test_greater():
       num = 100
>      assert num > 100
E      assert 100 > 100

ds/tests/test_compare.py:3: AssertionError
________________________________________________________________________________________ test_equality ________________________________________________________________________________________

    def test_equality():
>      assert 10 == 11
E      assert 10 == 11

ds/tests/test_square.py:12: AssertionError
=================================================================================== short test summary info ===================================================================================
FAILED ds/tests/test_compare.py::test_greater - assert 100 > 100
FAILED ds/tests/test_square.py::test_equality - assert 10 == 11
================================================================================= 2 failed, 4 passed in 0.03s =================================================================================
  1. 运行命令pytest test_square.py时,pytest提示找不到文件,未收集到测试用例,输出如下:
(pytest) bob@Roberts-Mac-mini pytest_tut % pytest test_square.py
===================================================================================== test session starts =====================================================================================
platform darwin -- Python 3.11.0, pytest-7.2.0, pluggy-1.0.0
rootdir: /Users/bob/Documents/work/pytest_tut
collected 0 items                                                                                                                                                                             

==================================================================================== no tests ran in 0.00s ====================================================================================
ERROR: file or directory not found: test_square.py

问题原因及解决方法

操作错误在于指定测试文件时路径不正确:test_square.py不在项目根目录下,而是存放在tests子目录中。

直接运行pytest时,pytest会默认递归扫描当前目录及所有子目录下符合test_*.py或*_test.py命名规则的测试文件,因此能找到tests目录下的文件。但指定文件名时,pytest只会在当前工作目录(项目根目录)下查找该文件,找不到就会报错。

正确的运行命令应为:

pytest tests/test_square.py

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 21:30:42