Unit 9 Stubs, Fakes, and Mocks Flashcards
Implement a testing suite using setUp and tearDown
Implement a testing suite using Mocks
Identify when using Mocks is best suited
What is the difference between setUp/tearDown and setUpClass/tearDownClass?
What is the difference between a Stub and a Mock?
What does Unittest’s patch do?
Context: Mocks
____ contain predefined data that is returned when called, but do not imitate behavior.
Stubs
Context: Mocks
___ simulate the behavior of a service and its actions can be verified.
Mocks
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”
return_value
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)
call(‘Greetings Planet’)
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()
side_effect
Context: Mocks
If we want to override the behavior of an imported item, we need to use patch
True
Context: Testing Environments
Which of the following is called before each test case in a TestCase object?
setUp()
Context: Testing Environments
Which of the following is called after all the test cases in a TestCase Object are run (not after each test)?
tearDownClass()
Context: Testing Environments
The @classmethod used with setUpClass() and tearDownClass() is called a modifier.
False