繁体   English   中英

如果没有明确的根包或模块,如何从 pytest 访问方法

[英]How to access a method from pytest, if there's no explicit root package or module

简单来说,我有这个目录结构:

src/
  my_file1.py

tests/
  __init__.py
  my_file1_test.py

my_file1.py

def my_func1(a):
  return a + 99

然后我如何从测试中访问 my_func1 ? 在 my_file1_test.py 中,我无法访问该方法:

# from ??? import ?? # is this needed at all?

def my_test1():
  res = my_func1(123) # unaccessible
  assert res = 222

我必须先在scr目录中创建__init__.py吗?

更新1

from src.myfile import my_func1

===>

ModuleNotFoundError: No module named 'scr' 

如果我添加__init__.py那么它会变成:

ImportError: cannot import name 'my_func1' from 'src' 

如果您从项目根目录运行 pytest

python -m pytest

然后你必须像你猜的那样导入函数:

from src.myfile import my_func1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM