本文介绍了将 pytest 与 src 层一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

pytest 建议包括用于分隔项目中源代码的附加目录:

pytest recommends including an additional directory to separate the source code within a project:

my_package
├── src  # <-- no __init__.py on this layer
│   └── my_package
│       ├── __init__.py
│       └── util_module
│           ├── __init__.py
│           └── utils.py
└── tests
    ├── __init__.py
    └── test_util_module
        ├── __init__.py
        └── test_utils.py

遗憾的是,他们没有说明在这种情况下测试代码中的导入应该如何工作,这对我的 IDE 在 这个简单的例子,但会导致pytest出现以下错误:

Sadly, they say nothing about how imports in the test code should work in such a case, which work for my IDE just fine in this naive example, but causes the following error with pytest:

my_package $ pytest

====================== test session starts ======================
platform linux -- Python 3.6.4, pytest-3.5.1, py-1.5.3, pluggy-0.6.0
rootdir: /home/user/workspace/my_package, inifile:
collected 0 items / 1 errors

============================ ERRORS =============================
___ ERROR collecting tests/test_util_module/test_utils.py ___
ImportError while importing test module '/home/user/workspace/my_package/tests/test_util_module/test_utils.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
tests/test_util_module/test_utils.py:1: in <module>
    from test.test_module.some_file import starify
E   ModuleNotFoundError: No module named 'my_package.util_module'
!!!! Interrupted: 1 errors during collection !!!!!

我可以通过将测试的导入更改为 from src.my_package.util_module.utils import starify 来解决此问题,但随后我的 IDE 对 src 部分提出了投诉多余,所以我想保留它.

I can fix the issue by changing the import of the test to from src.my_package.util_module.utils import starify, but then my IDE complaints about the src part being redundant, so I'd like to keep it out.

推荐答案

调整 PYTHONPATH(如评论中所建议)是解决导入问题的一种可能性.另一个是在 src 目录中添加一个空的 conftest.py 文件:

Adjusting the PYTHONPATH (as suggested in the comments) is one possibility to solve the import issue. Another is adding an empty conftest.py file in the src directory:

$ touch src/conftest.py

pytest 会将 src 添加到 sys.path.这是一种欺骗 pytest 将代码库添加到 sys.path 的简单方法.

and pytest will add src to sys.path. This is a simple way to trick pytest into adding codebase to sys.path.

但是,当您打算构建发行版时,通常会选择 src 布局,例如提供一个 setup.py 并(在本例中)明确指定根包目录:

However, the src layout is usually selected when you intend to build a distribution, e.g. providing a setup.py with (in this case) explicitly specifying the root package dir:

from setuptools import find_packages, setup


setup(
    ...
    package_dir={'': 'src'},
    packages=find_packages(where='src'),
    ...
)

并在您仍在开发时以开发模式(通过 python setup.py developpip install --editable .)安装包.这样,您的包 my_package 已正确集成到 Python 的站点包结构中,并且无需摆弄 PYTHONPATH.

and installing the package in the development mode (via python setup.py develop or pip install --editable .) while you're still developing it. This way, your package my_package is correctly integrated in the Python's site packages structure and there's no need to fiddle with PYTHONPATH.

这篇关于将 pytest 与 src 层一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-18 14:54
查看更多