【发布时间】:2026-02-11 03:45:01
【问题描述】:
嗨,健康的程序员,
我最近参加了很多实践活动。我一直在学习 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)
【问题讨论】:
-
你能告诉我们
response.data是什么吗? -
@waqasgard 响应数据是字典对象“Blogs”的形式例如:
b'{"Python Application Programming": "This article talks about Python and illustrates how Python is used in application programming, with examples."}\n' -
您能否确认响应确实包含您要断言的内容?
-
@waqasgard: 是的,我确认它可用.. b'{"PYTHON APPLICATION PROGRAMMING": "本文讨论了 Python,并通过示例说明了 Python 在应用程序编程中的使用方式。"}\n ' 不确定为什么即使响应正确,PUT 方法也没有被断言。
标签: python python-3.x list rest flask