1

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?

3
  • How do you mock other functions that get user input? Wouldn't this be the same? Commented Jun 3, 2022 at 0:27
  • 1
    see stackoverflow.com/questions/47690020/… Commented Jun 3, 2022 at 0:35
  • Thanks for linking that issue above, since I have a class I need to instantiate, how would i deal with the "module_under_test" value? Would this be the class instantiated with the method or just the class? I tried both ways and can't seem to get it to work. Commented Jun 3, 2022 at 13:47

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.