Practice Top 30 Python Programming Questions on Testing, TechnoVlogs

Practice Top 30 Python Programming Questions on Testing


Q1. Write a function to add two numbers. Write a unit test to verify the function's correctness.

Input: a=3, b=5
Expected Output: 8

Q2. Write a function to reverse a string. Write a test to check if the reversal is correct.

Input: s="hello"
Expected Output: "olleh"

Q3. Write a function to divide two numbers and test if it raises a ZeroDivisionError for division by zero.

Input: a=10, b=0
Expected Output: ZeroDivisionError

Q4. Write a function that returns a sorted version of a list. Write tests to check if the returned list matches the expected list.

Input: lst=[3, 1, 2]
Expected Output: [1, 2, 3]

Q5. Write a test case to check if the function sort_list handles an empty list correctly.

Input: lst=[]
Expected Output: []

Q6. Write 3 tests to check addition, subtraction, and multiplication functions in the same module. Verify test discovery using unittest.

Q7. Write a test case to verify if a file named data.txt exists in the current directory.

Q8. Write a function that adds a key-value pair to a dictionary. Write a test to verify the updated dictionary.

Input: d={}, key="name", value="John"
Expected Output: {"name": "John"}

Q9. Write a test to verify if a substring exists in a given string.

Input: string="hello world", substring="world"
Expected Output: True

Q10. Write a test using mocking to verify if the input function returns a specified value.

Q11. Write a test to verify a function with default parameters.

Input: None
Expected Output: "Hello, Guest!"

Q12. Mock a function get_weather() that fetches weather details and test if it returns mocked data.

Q13. Write a test to check if a function correctly returns True or False.

Input: num=4
Expected Output: True

Q14. Write a function to get the length of a string. Test the function.

Input: s="Python"
Expected Output: 6

Q15. Use mocking to verify the number of times a function is called.

Q16. Write a test to verify that a class initializes with correct attributes.

Input: name="Alice", age=25
Expected Output: Person object with name="Alice" and age=25

Q17. Write a test to check if multiple values in a function are as expected.

Q18. Write a function to return the current time. Mock it to return a specific time in tests.

Q19. Write a test to check if data is correctly written to a file.

Q20. Write a function to fetch a value from a dictionary and test if it raises KeyError for missing keys.

Q21. Write a function to convert a dictionary to JSON. Test it using the json module.

Q22. Write a test to check a function with multiple input sets using @parameterized.expand.

Q23. Write a test for a function that calls another function internally.

Q24. Mock a database query function and verify its result.

Q25. Write a custom exception and test if it is raised correctly in a function.

Q26. Write a test to check if a function correctly generates numbers in a range.

Q27. Write a test to verify a function’s behavior with an empty string input.

Q28. Write a test to mock and patch an attribute of an object dynamically.

Q29. Write a test that skips execution based on a condition.

Q30. Write a function to verify if a number is in a specified range. Test this function.

Input: num=5, start=1, end=10
Expected Output: True

Share on Social Media