Mocks¶
Mocking within the same module¶
To indicate that mocked object is in the same module as procedure being tested prepend the name of it with dot.
def increment_input():
"""
spec:
title: Incrementation of input
from unittest.mock import MagicMock
domain:
title: Should return 4 when input is 3
mock .input as MagicMock(return_value='3')
results 4
"""
return int(input()) + 1
Mocking external module¶
Let’s assume you want to reuse procedure defined in previous paragraph. To do that you have to specify absolute path to input. In this case mocked procedure is contained within examples.mocks.mock_input module. Note that you don’t have to import Mock to scope since it is already builtin.
from examples.mocks.mock_input import increment_input
from examples.result_validation.factorial import factorial
def mock_external():
"""
spec:
mock examples.mocks.mock_input.input as Mock(return_value=1)
domain : results 2
mock examples.mocks.mock_input.input as Mock(return_value=5)
domain : results 720
"""
value = increment_input()
return factorial(value)