Pytest 最小工作示例:收集了测试但找不到模块。配置测试集合的最佳方法是什么?

Pytest minimal working example: tests are collected but modules cannot be found. What is the best way to configure the test collection?

上下文

我正在尝试与其他人一起测试提供给我的项目,但我不确定如何进行。我已将其归结为该问题中显示的最小工作示例。

我的项目结构如下:

.
├── Problem1
│   ├── p1.py
│   └── tests
│       └── test_p1.py
└── Problem2
    ├── p2.py
    └── tests
        └── test_p2.py

p1.py

的内容
def add_two_numbers(x1, x2):
    return x1+x2

测试内容_p1.py

import pytest
from p1 import add_two_numbers

def test_add_two_numbers():
    assert add_two_numbers(1,2)==3

p2.py

的内容
def subtract_two_numbers(x1,x2):
    return x1-x2

测试内容_p2.py

import pytest
from p2 import subtract_two_numbers

def test_subtract_two_numbers():
    assert subtract_two_numbers(5,2)==3

问题

运行 pytestProblem1Problem2 目录中成功收集并 运行s test_p1.pytest_p1.py 中包含的测试test_p2.py,分别。

但是,我对 运行直接从根目录进行这些测试很感兴趣。这会导致导入问题并且 pytest 失败:

=============================================================================== test session starts ===============================================================================
platform linux -- Python 3.8.6, pytest-6.2.3, py-1.10.0, pluggy-0.13.1
rootdir: /home/awa5114/code/awa5114/example
plugins: dash-1.20.0, anyio-2.2.0
collected 0 items / 2 errors

===================================================================================== ERRORS ======================================================================================
___________________________________________________________________ ERROR collecting Problem1/tests/test_p1.py ____________________________________________________________________
ImportError while importing test module '/home/awa5114/code/awa5114/example/Problem1/tests/test_p1.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
../../../.pyenv/versions/3.8.6/lib/python3.8/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
Problem1/tests/test_p1.py:2: in <module>
    from p1 import add_two_numbers
E   ModuleNotFoundError: No module named 'p1'
___________________________________________________________________ ERROR collecting Problem2/tests/test_p2.py ____________________________________________________________________
ImportError while importing test module '/home/awa5114/code/awa5114/example/Problem2/tests/test_p2.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
../../../.pyenv/versions/3.8.6/lib/python3.8/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
Problem2/tests/test_p2.py:2: in <module>
    from p2 import subtract_two_numbers
E   ModuleNotFoundError: No module named 'p2'
============================================================================= short test summary info =============================================================================
ERROR Problem1/tests/test_p1.py
ERROR Problem2/tests/test_p2.py
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 2 errors during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
================================================================================ 2 errors in 0.07s ================================================================================

如何从根目录 运行 pytest 并确保找到模块?

我会说问题是你做相对进口,像这样:

from p1 import add_two_numbers

如果您随后从根文件夹中 运行 pytest 命令,pytest 将无法找到您的模块。也许绝对导入可以解决您的问题?

from Problem1.p1 import add_two_numbers

我使用与您相同的 folder/project 结构,并且我更喜欢绝对导入而不是相对导入。对我来说,我的方法有效。