How do you test a python function with side effects? (AI) Flashcards

(12 cards)

1
Q

What is the ideal characteristic of a function in Python regarding side effects?

A

A function should be pure, avoiding side effects

Pure functions return the same output for the same input without altering any external state.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the first strategy for testing functions with side effects?

A

Trace and Assert

This strategy is useful when expecting a particular side effect, such as an exception or new file creation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does the Temperament approach involve when testing functions with side effects?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the purpose of using mocks in testing functions with side effects?

A

To isolate code from its side effects

This enhances predictability in tests.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What should be the focus of IO-Centric Testing?

A

Input and output behaviors

This is particularly important for functions that are burdened by I/O operations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the first step in testing a python function with side effects?

A

Identify the Side Effect

Pinpoint the external interaction within your function, such as calling open() to write a file or modifying a global variable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What should you do after identifying the side effect in a function?

A

Mock the Dependency

Use Python’s built-in unittest.mock library or pytest-mock to replace the external dependency with a mock object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How can you control the behavior of a mock object in testing?

A

Control the Mock’s Behavior

Configure the mock to return specific values or raise exceptions that mimic different scenarios.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the purpose of making assertions on the mock in testing?

A

Make Assertions on the Mock

Verify that the function under test interacted with the mock object as expected, using assertions like assert_called_with().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Fill in the blank: The second step in testing a python function with side effects is to _______.

A

Mock the Dependency

This involves replacing the external dependency with a mock object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

True or false: You should never control the mock’s behavior when testing a function with side effects.

A

FALSE

Controlling the mock’s behavior is essential to simulate different scenarios during testing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What library can be used to mock dependencies in Python testing?

A

unittest.mock

This library allows you to create mock objects to simulate the behavior of real objects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly