Flask中HTTP PUT方法断言失败求助:8个测试用例仅第8个未通过
article_text in Response Looks like your PUT endpoint isn't returning the full blog object including the article_text field, which is why test case 8 is failing. Let's break down the issue and fix it step by step.
First, let's analyze the error
The test expects to find the article_text in the response data, but your API is only returning:
{"1": {"title": "PYTHON APPLICATION PROGRAMMING"}}
This points to two likely issues:
- Your PUT route isn't updating the
article_textin your data store (like an in-memory dict or database) - Even if it is updating the field, you're not including
article_textin the JSON response you send back.
Step 1: Check your PUT route in blogs_app.py
Chances are your current PUT handler looks something like this (the problematic version):
@app.route('/blogs/<int:blog_id>', methods=['PUT']) def update_blog(blog_id): # Assume 'blogs' is your in-memory storage if blog_id not in blogs: return jsonify({"error": "Blog not found"}), 404 update_data = request.get_json() # Only updating the title, ignoring article_text if "title" in update_data: blogs[blog_id]["title"] = update_data["title"] # Only returning the title, not the full blog object return jsonify({blog_id: {"title": blogs[blog_id]["title"]}})
Step 2: Fix the PUT route
You need to make two key changes:
- Handle the
article_textfield from the incoming request - Return the full updated blog object (not just the title)
Here's the corrected version:
@app.route('/blogs/<int:blog_id>', methods=['PUT']) def update_blog(blog_id): if blog_id not in blogs: return jsonify({"error": "Blog not found"}), 404 update_data = request.get_json() # Update title if provided if "title" in update_data: blogs[blog_id]["title"] = update_data["title"] # Update article_text if provided if "article_text" in update_data: blogs[blog_id]["article_text"] = update_data["article_text"] # Return the entire updated blog entry return jsonify({blog_id: blogs[blog_id]})
Step 3: Verify the test case request
Double-check test_app.py's test case 8 to make sure it's actually sending the article_text in the PUT request body. It should look like this:
def test_blogs_api_case8(self): target_url = '/blogs/1' payload = { "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.put(target_url, json=payload) # The assertion that was failing self.assertIn(b'"article_text": "This article talks about Python and illustrates how Python is used in application programming, with examples."', response.data)
If the test wasn't sending article_text, even a fixed endpoint wouldn't return it—so confirm this part is correct too.
After making these changes, re-run your tests, and test case 8 should pass since the response will now include the full blog object with both title and article_text.
内容的提问来源于stack exchange,提问作者Ashwin Kumar

