I have the following method in a class, that I am attempting to write a unit test for:
from getpass import getpass
class GetInfo:
def __init__(self, name):
self._name = name
def get_values(self):
if self._name == 'Bob':
phrase = getpass('What is your secret:')
new_dict = {self._name: phrase}
return new_dict
How would I write a unit test to verify the get_values method is functioning correctly? The piece I am most hung up on is how to mock the getpass value from the user. Thus far, this is what I have been able to write:
from getpass import getpass
from unittest.mock import patch
import mock
class TestInfo:
@mock.patch("getpass.getpass")
def test_get_credentials(self, password):
password.return_value = 'Password1'
info = GetInfo()
output_dict = info.get_values()
self.assertEqual(output_dict, {'Bob': 'Password1'})
if __name__ == '__main__':
test_main()
However; currently I am receiving an EOFError, signaling that I probably am not passing the value to the function at all. Would anyone be able to lend any insight?