Skip to main content
 首页 » 编程设计

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

2024年10月01日7tuyile006

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 中有 fixture 和函数 create_app()

但是这个 fixture 引发了一个错误

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.