nerdexam
Cisco

200-901 · Question #665

Drag and Drop Question Drag and drop the code snippets from the bottom onto the blanks in the Python script to construct a unit test that checks the API response. Not all options are used. Answer:

The correct answer is resp.status_code; resp.content_type; self.assertTrue. This question tests the ability to construct a Python unit test for an API by correctly selecting code snippets to extract response attributes and apply appropriate assertions using the unittest module.

Understanding and Using APIs

Question

Drag and Drop Question Drag and drop the code snippets from the bottom onto the blanks in the Python script to construct a unit test that checks the API response. Not all options are used. Answer:

Exhibit

200-901 question #665 exhibit

Answer Area

Drag items

resp.status_codeself.assertTrueresp.status_codesresp.content_typeself.assertFalseresp.content_data

Correct arrangement

  • resp.status_code
  • resp.content_type
  • self.assertTrue

Explanation

This question tests the ability to construct a Python unit test for an API by correctly selecting code snippets to extract response attributes and apply appropriate assertions using the unittest module.

Approach. 1. For the _test_status method: The line status_code = is followed by self.assertEqual(status_code, 200). This implies we need to assign the HTTP status code from the API response object (resp) to the status_code variable. The correct snippet for this is resp.status_code, which is a standard attribute for accessing the HTTP status code from a response object (e.g., from requests or werkzeug.test.TestResponse as returned by app.test_client()). 2. For the _test_content_type method: The line self.assertEqual(, 'application/json') is present. This assertion checks if a specific value is equal to the string 'application/json'. Given the method name, we are checking the content type of the response. The correct snippet to get the content type from the resp object is resp.content_type. 3. For the test_car_retrieval_with_id method: The line data = json.loads(resp.data) precedes a blank followed by (len(data) == 1). The expression len(data) == 1 evaluates to a boolean (True or False). To assert that this condition is true, the unittest module provides self.assertTrue(). Therefore, self.assertTrue is the correct snippet here.

Common mistakes.

  • common_mistake. 1. Using resp.status_codes: This attribute does not exist on standard response objects for getting the HTTP status code; resp.status_code is the correct attribute. resp.status_codes might be confused with a plural or a list, which is incorrect in this context.
  1. Using resp.content_data: This attribute does not exist for retrieving the Content-Type header. resp.content_type is the correct attribute for this purpose. resp.data (which is used earlier in the test_car_retrieval_with_id function) typically refers to the raw response body, not its type.
  2. Using self.assertFalse for len(data) == 1: If self.assertFalse(len(data) == 1) were used, it would mean the test expects the length of data not to be 1, which contradicts the likely intent of retrieving a single item by ID and generally leads to incorrect test logic given the context.
  3. Leaving snippets unused: The question explicitly states 'Not all options are used'. Incorrectly trying to use every snippet or selecting the wrong ones for the blanks due to misunderstanding the unittest assertion methods or response object attributes.

Concept tested. Python unit testing with the unittest module, specifically testing API responses. This includes understanding how to access HTTP status codes (resp.status_code), content types (resp.content_type), and raw response data (resp.data) from a response object (likely a werkzeug.test.TestResponse from a Flask test client), as well as utilizing unittest assertion methods like self.assertEqual and self.assertTrue to validate these aspects.

Topics

#Python Unit Testing#API Response Validation#unittest framework#HTTP Status Codes

Community Discussion

No community discussion yet for this question.

Full 200-901 Practice