POC POC Testing & Quality Assurance 2 — Questions and Answers
Question 1: In pytest, what built-in fixture provides a temporary directory for test files?
- tmp_path (Correct answer)
- tmpdir
- temp_dir
- test_dir
Correct answer: tmp_path
The `tmp_path` fixture in pytest provides a `pathlib.Path` temporary directory unique to each test invocation.
Question 2: Which method in unittest asserts that a specific exception is raised?
- assertRaises() (Correct answer)
- expectException()
- assertThrows()
- checkError()
Correct answer: assertRaises()
`assertRaises(ExceptionType)` is used as a context manager or callable to verify that the specified exception is raised.
Question 3: What is mocking in Python unit testing?
- Replacing real objects with simulated ones to isolate units under test (Correct answer)
- Creating test data files
- Running tests in parallel threads
- Generating HTML test reports
Correct answer: Replacing real objects with simulated ones to isolate units under test
Mocking replaces real dependencies with controlled substitutes so tests verify only the unit being tested.
Question 4: What does the `@pytest.fixture` decorator do?
- Defines a reusable setup function injected into test parameters (Correct answer)
- Marks a test as expected to fail
- Sets the test execution order
- Configures a test timeout value
Correct answer: Defines a reusable setup function injected into test parameters
The `@pytest.fixture` decorator defines a function that provides a fixed baseline or shared resource, injected by name into test function parameters.
Question 5: In unittest, what method is called after each test method to clean up resources?
- tearDown() (Correct answer)
- cleanup()
- reset()
- afterTest()
Correct answer: tearDown()
`tearDown()` is called after each test method to perform cleanup, even if the test raised an exception.
Question 6: What is code coverage in software testing?
- The percentage of source code lines executed during test runs (Correct answer)
- The number of test cases written
- The complexity score of test scenarios
- The volume of test documentation
Correct answer: The percentage of source code lines executed during test runs
Code coverage measures what percentage of source code is actually executed when the full test suite runs.
In pytest, what built-in fixture provides a temporary directory for test files?