[英]Running pytest with separate test files
I have a directory structure as recommended by the pytest good integration practices guide , which looks like this: 我有一个pytest良好集成做法指南推荐的目录结构,如下所示:
.
src/
mypkg/
__init__.py
mypkg.py
tests/
__init__.py
test_mypkg.py
The __init__.py
files are both empty, and the other files are as follows: __init__.py
文件均为空,其他文件如下:
mypkg.py
def foo(x):
"""
Show x.
>>> foo(5)
x is 5
>>> foo("hello")
x is hello
"""
return "x is {0}".format(x)
test.py
from mypkg import *
def test_foo():
assert foo(5) == "x is 5"
When I run pytest
from the root directory I get: 当我从根目录运行
pytest
,我得到:
tests\test_foo.py:1: in <module>
from mypkg import *
E ModuleNotFoundError: No module named 'mypkg'
What is the recommended way of setting this up? 推荐的设置方式是什么?
py doesn't know the path of mypkg.py . py不知道mypkg.py的路径。 So you need to inform that using below two lines
所以您需要使用以下两行来告知
import sys
sys.path.append("/path/to/src/mypkg")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.