如果没有明确的根包或模块,如何从 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