python - 烧瓶中的 Http PUT 方法在断言中失败
问题描述
嗨,健康的程序员,
我最近参加了很多实践活动。我一直在学习 Flask 编程并尝试在在线编程平台和挑战中找到的各种示例。我被困在一个解决方案中,其中我能够清除 7 个测试用例,而一个已经失败了很长时间。我无法在我编写的这个解决方案之外查看或设置我的想法。
有人可以帮助我破解这个 PUT http 测试用例吗?我附上了测试用例以及我的源代码。
blogs_app.py
from flask import Flask, request
from flask_restful import Resource, Api, abort
app = Flask(__name__)
api = Api(app)
blogs = {}
class BlogsAPI(Resource):
def get(self, blog_id=None):
if blog_id is None:
return blogs
if blog_id not in blogs:
abort(404,message="Blog_Id {} doesn't exist".format(blog_id))
return blogs[blog_id]
def post(self, blog_id):
if blog_id not in blogs:
title = request.form['title']
article_text = request.form['article_text']
created_at = '%Y-%m-%d %H:%M:%S'
blogs[blog_id] = {'title': title, 'article_text':article_text, 'created_at':created_at}
return {blog_id: blogs[blog_id]}
abort(404, message='Blog_Id {} already exists'.format(blog_id))
def put(self, blog_id):
if blog_id not in blogs:
abort(404,message="Blog_Id {} doesn't exist".format(blog_id))
blogs[blog_id] = request.form['title']
return {blog_id: blogs[blog_id]}
def delete(self, blog_id):
if blog_id in blogs:
response_string = 'Blog with Id {} is deleted'.format(blog_id)
del blogs[blog_id]
return response_string
abort(404, message="Blog_Id {} doesn't exist".format(blog_id))
api.add_resource(BlogsAPI, '/blogs/',
'/blogs/<int:blog_id>/')
if __name__ == '__main__':
app.run()
这是提供的测试用例文件。
test_app.py
from blogs_app import app
import unittest
from urllib import request
import datetime
class TestBlogsAPI(unittest.TestCase):
def setUp(self):
self.client = app.test_client()
def test_blogs_api_case1(self):
blog = {'title' : 'Python Application Programming',
'article_text': 'This article talks about Python and illustrates how Python is used in application programming, with examples.',
}
response = self.client.post('/blogs/1/', data=blog)
self.assertIn(b'"title": "Python Application Programming"', response.data)
self.assertIn(b'"article_text": "This article talks about Python and illustrates how Python is used in application programming, with examples."', response.data)
def test_blogs_api_case2(self):
blog = {'title' : 'Flask - A Micro Web Framework in Python',
'article_text': 'This article talks about Flask framework. It can be used to build web apps in a quick time.',
}
response = self.client.post('/blogs/2/', data=blog)
self.assertIn(b'"title": "Flask - A Micro Web Framework in Python"', response.data)
self.assertIn(b'"article_text": "This article talks about Flask framework. It can be used to build web apps in a quick time."', response.data)
def test_blogs_api_case3(self):
blog = {'title' : 'Flask-RESTful - Flask Extension for building REST APIs',
'article_text': 'This article introduces you to Flask-RESTful, an extension used for building REST APIs in Flask.',
}
response = self.client.post('/blogs/3/', data=blog)
self.assertIn(b'"title": "Flask-RESTful - Flask Extension for building REST APIs"', response.data)
self.assertIn(b'"article_text": "This article introduces you to Flask-RESTful, an extension used for building REST APIs in Flask."', response.data)
def test_blogs_api_case4(self):
response = self.client.get('/blogs/')
self.assertIn(b'"title": "Python Application Programming"', response.data)
self.assertIn(b'"article_text": "This article talks about Python and illustrates how Python is used in application programming, with examples."', response.data)
self.assertIn(b'"title": "Flask - A Micro Web Framework in Python"', response.data)
self.assertIn(b'"article_text": "This article talks about Flask framework. It can be used to build web apps in a quick time."', response.data)
self.assertIn(b'"title": "Flask-RESTful - Flask Extension for building REST APIs"', response.data)
self.assertIn(b'"article_text": "This article introduces you to Flask-RESTful, an extension used for building REST APIs in Flask."', response.data)
def test_blogs_api_case5(self):
response = self.client.get('/blogs/3/')
self.assertNotIn(b'"title": "Python Application Programming"', response.data)
self.assertNotIn(b'"article_text": "This article talks about Python and illustrates how Python is used in application programming, with examples."', response.data)
self.assertNotIn(b'"title": "Flask - A Micro Web Framework in Python"', response.data)
self.assertNotIn(b'"article_text": "This article talks about Flask framework. It can be used to build web apps in a quick time."', response.data)
self.assertIn(b'"title": "Flask-RESTful - Flask Extension for building REST APIs"', response.data)
self.assertIn(b'"article_text": "This article introduces you to Flask-RESTful, an extension used for building REST APIs in Flask."', response.data)
def test_blogs_api_case5(self):
response = self.client.delete('/blogs/3/')
self.assertIn(b'Blog with Id 3 is deleted', response.data)
response = self.client.get('/blogs/')
self.assertIn(b'"title": "Python Application Programming"', response.data)
self.assertIn(b'"article_text": "This article talks about Python and illustrates how Python is used in application programming, with examples."', response.data)
self.assertIn(b'"title": "Flask - A Micro Web Framework in Python"', response.data)
self.assertIn(b'"article_text": "This article talks about Flask framework. It can be used to build web apps in a quick time."', response.data)
self.assertNotIn(b'"title": "Flask-RESTful - Flask Extension for building REST APIs"', response.data)
self.assertNotIn(b'"article_text": "This article introduces you to Flask-RESTful, an extension used for building REST APIs in Flask."', response.data)
def test_blogs_api_case6(self):
response = self.client.delete('/blogs/6/')
self.assertIn(b"Blog_Id 6 doesn't exist", response.data)
def test_blogs_api_case7(self):
blog = {'title' : 'PYTHON APPLICATION PROGRAMMING',
}
response = self.client.put('/blogs/7/', data=blog)
self.assertIn(b"Blog_Id 7 doesn't exist", response.data)
def test_blogs_api_case8(self):
blog = {'title' : 'PYTHON APPLICATION PROGRAMMING'}
response = self.client.put('/blogs/1/', data=blog)
self.assertIn(b'"title": "PYTHON APPLICATION PROGRAMMING"', response.data)
self.assertIn(b'"article_text": "This article talks about Python and illustrates how Python is used in application programming, with examples."', response.data)
我在运行时收到以下错误消息,并且第 8 个测试用例失败:(
test_blogs_api_case1 (test_app.TestBlogsAPI) ... ok
test_blogs_api_case2 (test_app.TestBlogsAPI) ... ok
test_blogs_api_case3 (test_app.TestBlogsAPI) ... ok
test_blogs_api_case4 (test_app.TestBlogsAPI) ... ok
test_blogs_api_case5 (test_app.TestBlogsAPI) ... ok
test_blogs_api_case6 (test_app.TestBlogsAPI) ... ok
test_blogs_api_case7 (test_app.TestBlogsAPI) ... ok
test_blogs_api_case8 (test_app.TestBlogsAPI) ... FAIL
======================================================================
FAIL: test_blogs_api_case8 (test_app.TestBlogsAPI)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/projects/challenge/test_app.py", line 81, in test_blogs_api_case8
self.assertIn(b'"article_text": "This article talks about Python and illustrates how Python is used in application programming, with examples."', response.data)
AssertionError: b'"article_text": "This article talks about Python and illustrates how Python is used in application programming, with examples."' not found in b'{"1": {"title": "PYTHON APPLICATION PROGRAMMING"}}\n'
----------------------------------------------------------------------
Ran 8 tests in 0.022s
FAILED (failures=1)
解决方案
def put(self, blog_id):
if blog_id not in blogs:
abort(404,message="Blog_Id {} doesn't exist".format(blog_id))
**blogs[blog_id]['title'] = request.form['title']**
return {blog_id: blogs[blog_id]}
改变 put 方法
推荐阅读
- javascript - 如何在 js 中对嵌套的事件监听器进行排序
- java - 在 Kubernetes 上启动 Quarkus 时出现 NumberFormatException
- angular - 如何在 ngx 数据表中对行进行分组
- scala - 在 Scala Spark、dataproc zeppelin notebook 中实现 XGBoost
- bash - 如何在我的 bash 脚本中打印 TCL 环境变量
- django - django 序列化程序中的自定义查询
- python - 当我在银行中增加索引时,为什么状态会发生变化?
- sql - CASE 子句中的 SQL WHERE 表达式
- sql - 使用 VBA 和 SQL 从一个 ACCESS 表删除和插入记录到另一个表
- pandas - 查找每天数据框列中的最小值