Unit 9 Stubs, Fakes, and Mocks Flashcards

1
Q

Implement a testing suite using setUp and tearDown

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

Implement a testing suite using Mocks

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

Identify when using Mocks is best suited

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

What is the difference between setUp/tearDown and setUpClass/tearDownClass?

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

What is the difference between a Stub and a Mock?

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

What does Unittest’s patch do?

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

Context: Mocks

____ contain predefined data that is returned when called, but do not imitate behavior.

A

Stubs

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

Context: Mocks

___ simulate the behavior of a service and its actions can be verified.

A

Mocks

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

Context: Mocks

Given the following code snippet, what goes in the blank?

from unittest.mock import Mock

mock = Mock()

# Set return_value
mock.abs.______ = “7”

A

return_value

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

Context: Mocks

Given the following code snippet, what is the output of the print statement?

mock = Mock()

mock.func(“Hello World”)
mock.func(“Greetings Planet”)

print(mock.func.call_args)

A

call(‘Greetings Planet’)

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

Context: Mocks

Given the following snippet. If we wanted to use a mock to trigger an exception, what would we use in the blank?

Mock file reader to control its behavior

open = Mock()

def load_file():
# Does absolutely nothing other than raise the desired exception
content = open(‘temp_file.txt’)
if content.length != 0:
return content
return None

class TestCase(unittest.TestCase):
def test_read_file(self):
# Test for IOError
open.______ = IOError
# This is a context manager that allows us to test for exceptions
with self.assertRaises(IOError):
load_file()

A

side_effect

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

Context: Mocks

If we want to override the behavior of an imported item, we need to use patch

A

True

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

Context: Testing Environments

Which of the following is called before each test case in a TestCase object?

A

setUp()

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

Context: Testing Environments

Which of the following is called after all the test cases in a TestCase Object are run (not after each test)?

A

tearDownClass()

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

Context: Testing Environments

The @classmethod used with setUpClass() and tearDownClass() is called a modifier.

A

False

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