我的Flask应用程序具有以下目录结构
.
├── app
│ ├── api
│ │ ├── errors.py
│ │ └── __init__.py
│ ├── errors
│ │ ├── handlers.py
│ │ └── __init__.py
│ ├── __init__.py
│ └── main
│ ├── __init__.py
│ └── routes.py
├── config.py
├── main.py
├── README.md
├── requirements.txt
└── tests
├── conftest.py
├── functional
│ ├── __init__.py
└── └── test_errors.py
conftest.py
的外观
import pytest
from app import create_app, db
from config import Config
class TestConfig(Config):
TESTING = True
SQLALCHEMY_DATABASE_URI = "sqlite://"
LOG_TO_STDOUT = False
@pytest.fixture(scope="module")
def test_client():
flask_app = create_app(TestConfig)
# Flask provides a way to test your application by exposing the Werkzeug test Client
# and handling the context locals for you.
testing_client = flask_app.test_client()
# Establish an application context before running the tests.
ctx = flask_app.app_context()
ctx.push()
yield testing_client # this is where the testing happens!
ctx.pop()
但是,当我在根目录中并运行pytest
时,会收到错误消息
ImportError while loading conftest '/home/user/Documents/flask-api-app/tests/conftest.py'.
tests/conftest.py:2: in <module>
from app import create_app, db
E ModuleNotFoundError: No module named 'app'
,这必须意味着测试不是从根目录运行的。我想念什么吗?我正在尝试关注Testing a Flask Application using pytest by Pat Kennedy
中的博客文章