Python Programming - Testing

Exercise : Testing - General Questions
  • Testing - General Questions
1.
How can you check if a Python function raises a specific exception during testing?
Use the assertRaises method from the unittest module
Utilize the expectException decorator
Employ the checkException function in the testing module
Insert a try-except block and manually check for the exception
Answer: Option
Explanation:
The assertRaises method in the unittest module is used to check if a specific exception is raised by a Python function during testing.

2.
What is the purpose of the setUp method in a Python test class?
It signals the start of the test class
It initializes resources before each test method is executed
It marks a test as skipped
It defines the teardown logic for a test case
Answer: Option
Explanation:
The setUp method in a Python test class is used to initialize resources or perform setup tasks before each test method is executed.

3.
Which assertion method in the unittest module is used to check if two values are not equal?
assertEqual
assertNotEqual
assertTrue
assertNot
Answer: Option
Explanation:
The assertNotEqual method in the unittest module is used to check if two values are not equal during testing.

4.
What is the purpose of the tearDown method in a Python test class?
It signals the end of the test class
It initializes resources before each test method is executed
It marks a test as skipped
It defines the teardown logic for a test case
Answer: Option
Explanation:
The tearDown method in a Python test class is used to define the teardown or cleanup logic that should be executed after each test method.

5.
What is the purpose of the @classmethod decorator in a Python test class?
It marks a method as a class method
It signals that the method is a test case
It defines the setup logic for a test case
It is not used in test classes
Answer: Option
Explanation:
The @classmethod decorator in a Python test class is used to mark a method as a class method, allowing it to be called on the class itself rather than an instance.