我正在使用 Python 3.8 和 Azure 函数。我有以下项目布局......
myproject
__app__
__init__.py
objects
__init__.py
tests
__init__.py
functions
__init__.py
objects
__init__.py
test_objects.py
我的app/objects/init.py 文件是这样开始的......
import azure.functions as func
def main(req: func.HttpRequest) -> func.HttpResponse:
"""."""
...
然后我的 tests/functions/objects/init.py 看起来像
import pytest
import azure.functions as func
_import = __import__('__app__/objects')
def test_objects():
...
但是,当我运行的时候
pytest tests/functions/test_objects.py
我得到了错误
tests/functions/test_objects.py:8: in <module>
_import = __import__('__app__/objects')
E ModuleNotFoundError: No module named '__app__/objects'
引用我的函数的正确方法是什么?我也只尝试了“对象”,但这导致了相同的 ModuleNotFoundError。
最佳答案
来自docs我看到函数是直接导入的,即:
# tests/test_httptrigger.py
import unittest
import azure.functions as func
from __app__.HttpTrigger import my_function
也许您可以尝试像这样引用您的测试:
# tests/test_objects.py
import unittest
import azure.functions as func
from __app__.objects import main
+++更新+++
现在我尝试自己做,但偶然发现了类似的错误。经过一番谷歌搜索后,我还在 github.com/Azure/azure-functions-python-worker 找到了一个 Unresolved 问题。 .
+++ 更新 2+++
我已经设法通过将一个空的 __init__.py
文件添加到测试文件夹来让它运行。另一件事是,我已经适应了上述文档中的文件夹结构,并在根文件夹中运行了以下请求:
pytest.
结果,测试执行成功。这是我创建的示例存储库: github.com/DSpirit/azure-functions-python-unit-testing
关于python-3.x - 如何从 pytest 引用我的 Azure 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64377567/