我尝试使用文件夹结构来组织Python项目。当我需要进行测试时,我使用如下内容。
.
|-- src
| |-- b.py
| `-- main.py
`-- tests
`-- test_main.py
这种方法只有一个大问题。如果main.py
导入b.py
,Pytest就不会运行。
到目前为止,我已经尝试将空的__init__.py
文件放置在src
和tests
文件夹中,无论是独立的还是一起的,但是其中任何一个似乎都能工作。
在我看来,这是一个相当标准的项目,但我一直未能在网上找到解决方案。我应该使用不同的文件夹结构吗?对于这类项目,有没有推荐使用pytest的方法?
这是文件的内容:
# b.py
def triplicate(x):
return x * 3
# main.py
from b import triplicate
def duplicate(x):
return x * 2
# test_main.py
from src.main import duplicate
def test_duplicate():
assert duplicate(2) == 4
这就是我在运行pytest时遇到的错误:
==================================================================================================== ERRORS ====================================================================================================
_____________________________________________________________________________________ ERROR collecting tests/test_main.py ______________________________________________________________________________________
ImportError while importing test module 'C:\Users\edwar\test_pytest\tests\test_main.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
c:\python39\lib\importlib\__init__.py:127: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
tests\test_main.py:1: in <module>
from src.main import duplicate
src\main.py:1: in <module>
from b import triplicate
E ModuleNotFoundError: No module named 'b'
=========================================================================================== short test summary info ============================================================================================
ERROR tests/test_main.py
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
=============================================================================================== 1 error in 0.15s ===============================================================================================
发布于 2021-07-21 23:19:03
Python使用“环境变量”PYTHONPATH
来查找从其中导入代码的源代码。默认情况下,自动包含执行python程序的目录,但在测试时要包括这样的内容:
PYTHONPATH=$PYTHONPATH,../src python test_main.py
这是如果您要从源目录执行测试的话。像IntelliJ (PyCharm)这样的工具将允许您在测试调用中将其作为一个值添加。或者,您可以使用export PYTHONPATH=...
。(请注意,这是一个*nix环境,您在windows上的里程可能会有所不同。)
其结果是,PYTHONPATH
中的每个目录都将被加载,并且将尝试将其用作您试图导入的模块的“根”。基本的目录结构是最惯用的。
发布于 2021-07-21 23:11:32
您可以将源文件放在根目录中,并调整导入路径如下:
main.py
# main.py
from stackoverflow.b import triplicate
def duplicate(x):
return x * 2
b.py
# b.py
def triplicate(x):
return x * 3
test_main.py
# test_main.py
from stackoverflow.main import duplicate
def test_duplicate():
assert duplicate(2) == 4
然后从根目录pytest .
运行stackoverflow
。
collected 1 item
tests\test_main.py . [100%]
====================================================================================== 1 passed in 0.03s =======================================================================================
或者,如果希望保留src
文件夹,则导入如下:
from stackoverflow.src.b import triplicate
https://stackoverflow.com/questions/68476886
复制相似问题