python-3.x - 如何从 pytest 引用我的 Azure 函数?

标签 python-3.x import azure-functions pytest python-import

我正在使用 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/

相关文章:

unit-testing - 如何在golang中测试主要的包功能?

azure - 无法使用 Visual Studio 发布 Azure Function : Create button disabled

c# - 是否可以从Azure功能所在的同一文件夹中读取文件

azure - 将字符串参数绑定(bind)到 Azure Function 中的 Enum 类型

python - "argument 1 has unexpected type ' 字符串 '"

python-3.x - 使用 asyncio 实现类似选择的功能

Python 脚本在 IDLE 中执行时有效,但在控制台中无效

python - 来自 itertools.cycle 生成器的列表理解

javascript - 在导入语句的上下文中,子路径是什么意思?

css - CSS 中@import 和 link 的区别