Created
September 24, 2019 11:22
-
-
Save mauricioaniche/9449088438d5562829c4c911d7c043fd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def my_example_function(): | |
print("Hi") | |
name = input("Tell me your name") | |
age = input("Tell me your age") | |
print("Hi {}, you are {} years old".format(name, age)) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from project.example import my_example_function | |
from project.test.tud_test_base import mock_input_output_start, mock_input_output_end, set_input, get_output | |
def test_1(): | |
mock_input_output_start() | |
set_input(["Mauricio", 33]) | |
my_example_function() | |
output = get_output() | |
assert output == ["Hi","Tell me your name", "Tell me your age", "Hi Mauricio, you are 33 years old"] | |
mock_input_output_end() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import builtins | |
input_values = [] | |
print_values = [] | |
previous_input = None | |
previous_print = None | |
def mock_input(s): | |
print_values.append(s) | |
return input_values.pop(0) | |
def mock_input_output_start(): | |
global input_values, print_values, previous_input, previous_print | |
input_values = [] | |
print_values = [] | |
previous_input = builtins.input | |
previous_print = builtins.print | |
builtins.input = mock_input | |
builtins.print = lambda s: print_values.append(s) | |
def mock_input_output_end(): | |
builtins.input = previous_input | |
builtins.print = previous_print | |
def get_output(): | |
global print_values | |
return print_values | |
def set_input(mocked_inputs): | |
global input_values | |
input_values = mocked_inputs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment