What is the ideal characteristic of a function in Python regarding side effects?
A function should be pure, avoiding side effects
Pure functions return the same output for the same input without altering any external state.
What is the first strategy for testing functions with side effects?
Trace and Assert
This strategy is useful when expecting a particular side effect, such as an exception or new file creation.
What does the Temperament approach involve when testing functions with side effects?
Finding the balance between mocking and not mocking
You might mock an expensive process for speed or avoid mocking if the process is simple and deterministic.
What is the purpose of using mocks in testing functions with side effects?
To isolate code from its side effects
This enhances predictability in tests.
What should be the focus of IO-Centric Testing?
Input and output behaviors
This is particularly important for functions that are burdened by I/O operations.
What is the first step in testing a python function with side effects?
Identify the Side Effect
Pinpoint the external interaction within your function, such as calling open() to write a file or modifying a global variable.
What should you do after identifying the side effect in a function?
Mock the Dependency
Use Python’s built-in unittest.mock library or pytest-mock to replace the external dependency with a mock object.
How can you control the behavior of a mock object in testing?
Control the Mock’s Behavior
Configure the mock to return specific values or raise exceptions that mimic different scenarios.
What is the purpose of making assertions on the mock in testing?
Make Assertions on the Mock
Verify that the function under test interacted with the mock object as expected, using assertions like assert_called_with().
Fill in the blank: The second step in testing a python function with side effects is to _______.
Mock the Dependency
This involves replacing the external dependency with a mock object.
True or false: You should never control the mock’s behavior when testing a function with side effects.
FALSE
Controlling the mock’s behavior is essential to simulate different scenarios during testing.
What library can be used to mock dependencies in Python testing?
unittest.mock
This library allows you to create mock objects to simulate the behavior of real objects.