python - 在 pytest 装置中导入应用程序时出错

标签 python pytest

client_fixture.py

import pytest

from app.apis import api
from app import create_app


@pytest.fixture
def app_client(credentials, redis_cred_setup):
    app = create_app(credentials['app_config_name'])
    api.init_app(app)

    client = app.test_client()
    return client

我在app包的__init__.py内有固定装置和函数create_app()

但是这个装置会引发错误

ImportError:导入插件“client_fixtures”时出错:没有名为“app”的模块

包结构

Core
    app-
        ...
        __init__.py

    tests-
        client_fixture.py

Maybe it will be helpful

当我使用 PyCharm 运行测试时,一切正常 我只需在测试文件夹上单击鼠标右键并执行“在测试中运行 py.test”

最佳答案

您需要调用pytest,如下所示:

python -m pytest tests/

来自documentation :

This is almost equivalent to invoking the command line script pytest tests/ directly, except that Python will also add the current directory to sys.path.

关于python - 在 pytest 装置中导入应用程序时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46315803/

相关文章:

python - 为什么 setdefault 在设置键时评估默认值?

python - python中的Mininet套接字编程

python - python 3.7 中 .append 函数的自动化

python - 如何在 Python 中模拟列表?

python - 阻止python关闭错误

python - 计算每个 bin 中 numpy ndarray 的最大值

python - Selenium - visibility_of_element_located : __init__() takes exactly 2 arguments (3 given)

python - pytest 是否限制自定义命令行参数中的所有小写选项?

python - 如果未安装可选模块,则测试代码的行为

python - Django:在单独的线程中使用相同的测试数据库