200-901 · Question #287
Drag and Drop Questions Drag and drop the code snippets from the bottom to the blanks in the code to test the API response through the Python unittest library. Not all options are used. Answer:
The correct answer is get; response; status_code. This question assesses the candidate's ability to complete Python unittest code for API testing, specifically focusing on verifying HTTP status codes, content types, and handling non-existent resources.
Question
Drag and Drop Questions Drag and drop the code snippets from the bottom to the blanks in the code to test the API response through the Python unittest library. Not all options are used. Answer:
Exhibit
Answer Area
Drag items
Correct arrangement
- get
- response
- status_code
Explanation
This question assesses the candidate's ability to complete Python unittest code for API testing, specifically focusing on verifying HTTP status codes, content types, and handling non-existent resources.
Approach. 1. For the first blank in self.assertEqual(status_code, _____) within test_status: The API call self.app.get('/dogs/all') is expected to be successful. A standard successful HTTP response status code is 200 (OK). Therefore, 200 is the correct snippet to drag here.
2. For the second blank in self.assertEqual(resp._____, 'application/json') within test_content_type: This test aims to verify that the API response's content type is application/json. API response objects typically expose a content_type attribute to access this information. Thus, content_type is the correct snippet.
3. For the third blank in resp = self.app.get('/dogs?id=_____') within test_item_not_exist: The objective of this test is to trigger a '404 Not Found' response, implying a request for a resource that does not exist. Passing none as an ID parameter is a common and appropriate way to simulate a request for a non-existent resource from the given options. Therefore, none is the correct snippet.
Common mistakes.
- common_mistake. 1. Using
403instead of200for the status code verification intest_status:403(Forbidden) indicates an authorization issue, not a successful API response, which typically uses200(OK).
- Using
contentorstatusinstead ofcontent_typefor content type verification:resp.contentwould retrieve the raw response body, whileresp.statusis not a standard attribute for content type. The correct attribute to check the MIME type of the response iscontent_type. - Using
status_codefor the ID parameter intest_item_not_exist:status_codeis an attribute of the response object, not a valid value to pass as an ID parameter to simulate a non-existent resource. Similarly,contentis also not appropriate as an ID.
Concept tested. This question tests the candidate's understanding of Python's unittest framework for creating and running tests, fundamental concepts of API testing including verifying HTTP status codes (e.g., 200 OK, 404 Not Found) and response content types, and how to interact with typical API response objects (e.g., status_code, content_type attributes) when using a test client.
Topics
Community Discussion
No community discussion yet for this question.
