200-901 exam dumps

200-901 practice question 116 of 204

Cisco DevNet Associate. Associate level, Cisco. Free question with the correct answer and a full explanation.

200-901 Question 116

Single answer

You are developing a Python application that interacts with a RESTful API. To ensure your code properly handles the API responses, you need to test a function, parse_response(data), that processes JSON data returned from the API. Which of the following demonstrates proper usage of Python's unittest module to test the function?

  1. A

    Define a test class that inherits from unittest.TestCase, define test methods starting with test_, and use assertions such as self.assertEqual to verify results.

  2. B

    Create a Python script that calls the parse_response function with sample data and prints the output for manual verification.

  3. C

    Write a test function starting with test_ and call assertions directly without using a test class.

  4. D

    Use the unittest.main() function to automatically discover and run all test scripts in the project.

Show answer and explanation

Correct answer: A

Explanation

Unit tests in Python using the unittest module follow a specific structure: a test class derived from unittest.TestCase, test methods starting with test_, and assertions to validate expected outcomes. This ensures a standardized, automated, and maintainable testing process.

  • A. Correct.

    Correct: To construct a proper unit test in Python using the unittest module, you need to define a class that inherits from unittest.TestCase, write test methods prefixed with test_, and use assertions to validate the behavior of the function being tested.

  • B. Incorrect.

    Incorrect: While printing output for manual verification can help during debugging, it is not considered a proper unit testing approach as it lacks automation and integration with testing frameworks.

  • C. Incorrect.

    Incorrect: Test functions should be part of a test class when using the unittest module. Writing standalone test functions is not aligned with unittest's structured approach.

  • D. Incorrect.

    Incorrect: unittest.main() is used to run the tests, but it does not replace the need for properly constructing test cases and test methods.

Timed practice exam

Take a 200-901 practice test under exam conditions

75 questions in 120 minutes, drawn from this bank, with a score report and a per-question review when you finish.

Start timed exam