pytest 找不到模块

pytest cannot find module

我正在关注pytest good practices,或者至少我认为我是。但是,pytest 找不到我的模块。它似乎没有在其 PYTHONPATH.

中包含当前目录

源文件:

def add(x, y):
    return x + y

测试文件:

import pytest
from junk.ook import add


def test_add_true():
    assert add(1, 1) == 2

并且 shell 输出带有一个名为“p3”的 Python 3 虚拟环境。

p3; pwd          
/home/usr/tmp/junk
p3; ls           
total 0
0 junk/  0 tests/
p3; ls junk      
total 4.0K
4.0K ook.py     0 __init__.py
p3; ls tests 
total 4.0K
4.0K test_ook.py     0 __pycache__/
p3; pytest
============================= test session starts ==============================
platform linux -- Python 3.4.5, pytest-3.4.1, py-1.5.2, pluggy-0.6.0
rootdir: /home/usr/tmp/junk, inifile:
collected 0 items / 1 errors                                                   

==================================== ERRORS ====================================
______________________ ERROR collecting tests/test_ook.py ______________________
ImportError while importing test module '/home/usr/tmp/junk/tests/test_ook.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
tests/test_ook.py:2: in <module>
    from junk.ook import add
E   ImportError: No module named 'junk'
!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!
=========================== 1 error in 0.08 seconds ============================
    
    def test_add_true():
        assert add(1, 1) == 2

但是,运行以下确实可以正常工作。

p3; python -m pytest tests/
============================= test session starts ==============================
platform linux -- Python 3.4.5, pytest-3.4.1, py-1.5.2, pluggy-0.6.0
rootdir: /home/usr/tmp/junk, inifile:
collected 1 item                                                               

tests/test_ook.py .                                                      [100%]

=========================== 1 passed in 0.02 seconds ===========================

我做错了什么?

只需将 __init__.py 添加到 tests 目录,并递归添加到其中包含测试文件的所有目录。

pytest 7 及更新版本的更新:使用 pythonpath 设置

最近,pytest 添加了一个新的核心插件,支持通过 pythonpath configuration value 进行 sys.path 修改。因此,现在的解决方案简单多了,不再需要任何解决方法:

pyproject.toml 示例:

[tool.pytest.ini_options]
pythonpath = [
  "."
]

pytest.ini 示例:

[pytest]
pythonpath = .

路径条目是相对于 rootdir 计算的,因此 . 在这种情况下将 junk 目录添加到 sys.path

也允许多个路径条目:对于布局

junk/
├── src/
|   └── lib.py
├── junk/
│   ├── __init__.py
│   └── ook.py
└── tests
     ├── test_app.py
     └── test_lib.py

配置

[tool.pytest.ini_options]
pythonpath = [
  ".", "src",
]

[pytest]
pythonpath = . src

会将 lib 模块和 junk 包添加到 sys.path,因此

import junk
import lib

两者都可以。

原回答

在项目根目录下放一个空的conftest.py文件即可:

$ pwd
/home/usr/tmp/junk
$ touch conftest.py

你的项目结构应该变成:

junk
├── conftest.py
├── junk
│   ├── __init__.py
│   └── ook.py
└── tests
    └── test_ook.py

这里发生了什么:当 pytest 发现 conftest.py 时,它会修改 sys.path 以便它可以从 conftest 模块导入内容。因此,由于现在在 rootdir 中发现了一个空 conftest.pypytest 将被迫将其附加到 sys.path。这样做的副作用是您的 junk 模块变得可导入。