python-3.x - Flask_testing 为看似正常工作的端点意外返回 404
问题描述
为我的烧瓶应用程序编写一些单元测试。当我在邮递员中尝试时,端点'/'有效并返回 200,但是 flask_testing 给出AssertionError: 404 != 200
我已经设置了一个基本配置。
class BaseTestCase(TestCase):
def create_app(self):
app = Flask(__name__)
app.config['TESTING'] = True
app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:///:memory:"
app.config['JWT_SECRET_KEY'] = environ['JWT_SECRET_KEY']
db = SQLAlchemy(app)
db.init_app(app)
return app
这是测试。
class FlaskTestCase(BaseTestCase):
def test_root(self):
response = self.client.get('/')
self.assertEquals(response.status_code, 200)
测试的输出
======================================================================
FAIL: test_root (test.server-test.FlaskTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/my_name/path/to/my_project/test/server-test.py", line 13, in test_root
self.assertEquals(response.status_code, 200)
AssertionError: 404 != 200
----------------------------------------------------------------------
Ran 1 test in 0.032s
FAILED (failures=1)
解决方案
在我的情况下,我在测试的路线中有这个 if 语句
if len(books_per_request) == 0:
response = make_response(jsonify(message="value exceeded books count"), 404)
虽然请求成功,但我在测试运行和计数 len == 0 时发送了 404 错误,如果您像我这样的第一次测试检查正常路线,如果一切正常,则返回正常字符串,检查您的家路线' /'
- 确保它存在于您的init .py 文件中
- 并确保它不返回任何 404 错误
我希望这对任何人都有帮助
推荐阅读
- python - 在 IIS 服务器上部署烧瓶 api 给出错误 0x8007010b 通知:ExecuteRequestHandler
- reactjs - React TypeError:无法读取 null 的属性“目标”
- css - 覆盖 all.sass 文件中的 Bulma 初始变量?
- python - 从 crontab 启动时出现 SSL 错误,但在手动启动时却没有——出了什么问题?
- node.js - 我如何从 Node js 服务器上传视频到 Vimeo
- c# - file.Exist 在 localhost 上返回 true 并在服务器 c# 上返回 false
- javascript - 猫鼬在枚举上使用过滤器查找不返回结果
- python - 从数据框中删除选择性行
- python - 如何使用python从get方法将值写入excel
- c# - C# SQLite 检索完整的 blob 并插入完整的 blob