作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用Flask Test tutorial为我的Flask应用程序创建测试,但是当我使用pytest运行测试时,会引发以下错误:
Hint: make sure your test modules/packages have valid Python names.
Traceback:
test\test_flask.py:6: in <module>
from app import app
E ModuleNotFoundError: No module named 'app'
import os
import tempfile
import pytest
from app import app
@pytest.fixture
def client():
db_fd, app.config['DATABASE'] = tempfile.mkstemp()
app.config['TESTING'] = True
with app.app.test_client() as client:
with app.app.context():
app.init_db()
yield client
os.close(db_fd)
os.unlink(app.app.config['DATABASE'])
from flask import Flask, request, jsonify
from config import Config
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from werkzeug.security import generate_password_hash, check_password_hash
from flask_jwt_extended import (
JWTManager, jwt_required, create_access_token,
get_jwt_identity
)
from flask_cors import CORS
app = Flask(__name__)
app.config.from_object(Config)
db = SQLAlchemy(app)
migrate = Migrate(app, db)
jwt = JWTManager(app)
CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
from app import routes, models
from app import app
最佳答案
我解决了在项目的根目录中创建setup.py并构建我的包的问题,以便测试目录可以访问应用程序包。
setup.py:
import setuptools
with open("README.md", "r") as fh:
long_description = fh.read()
setuptools.setup(
name="appPackage-YourUser",
version="0.0.1",
author="Your Name",
author_email="yourownemail@email.com",
description="",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/luturol/MyManhuaListAPI",
packages=setuptools.find_packages(),
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
python_requires='>=3.6',
)
pip install -e .
关于python - ModuleNotFoundError : No module named 'app' when trying to create a Test using Pytest and Flask on Windows virtual enviroment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60859997/
我是一名优秀的程序员,十分优秀!