I need to test a function that handles inputs. I'm a newbie in all about unittests and mocks so I just basically followed this answer https://stackoverflow.com/a/21047132/6531256 that it looks very similar to my case. Well, the problem is that when i run the test it seems to start but just stay there and nothing happens. I need to quit with ctrl-c to stop it, then I get a traceback that not helps very much(at least for me).
Here is the test code:
import unittest
import unittest.mock
from unittest.mock import patch
from work1 import User
class TestWork1(unittest.TestCase, User):
@patch('builtins.input', return_value= "36")
def test_userNum(self,return_value):
self.assertEqual(self.userNum(), "Invalid number. Put a 4-digit number:")
if __name__ == "__main__":
unittest.main()
Here the function trying to test:
class User():
def userNum(self):
self.user_num = int(input("Put a 4-digit number"))
while len(str(self.user_num)) != 4:
self.user_num = int(input("Invalid number. Put a 4-digit number:"))
Here is an example of a Traceback (always are a little diferent):
Traceback (most recent call last):
File "/usr/lib/python3.5/runpy.py", line 184, in _run_module_as_main
"__main__", mod_spec)
File "/usr/lib/python3.5/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/usr/lib/python3.5/unittest/__main__.py", line 18, in <module>
main(module=None)
File "/usr/lib/python3.5/unittest/main.py", line 94, in __init__
self.runTests()
File "/usr/lib/python3.5/unittest/main.py", line 255, in runTests
self.result = testRunner.run(self.test)
File "/usr/lib/python3.5/unittest/runner.py", line 176, in run
test(result)
File "/usr/lib/python3.5/unittest/suite.py", line 84, in __call__
return self.run(*args, **kwds)
File "/usr/lib/python3.5/unittest/suite.py", line 122, in run
test(result)
File "/usr/lib/python3.5/unittest/suite.py", line 84, in __call__
return self.run(*args, **kwds)
File "/usr/lib/python3.5/unittest/suite.py", line 122, in run
test(result)
File "/usr/lib/python3.5/unittest/suite.py", line 84, in __call__
return self.run(*args, **kwds)
File "/usr/lib/python3.5/unittest/suite.py", line 122, in run
test(result)
File "/usr/lib/python3.5/unittest/case.py", line 648, in __call__
return self.run(*args, **kwds)
File "/usr/lib/python3.5/unittest/case.py", line 600, in run
testMethod()
File "/usr/lib/python3.5/unittest/mock.py", line 1157, in patched
return func(*args, **keywargs)
File "/home/cristian/venvs/EB/test_work1.py", line 15, in test_userNum
self.assertEqual(self.userNum(), "Invalid number. Put a 4-digit number:")
File "/home/cristian/venvs/EB/work1.py", line 15, in userNum
self.user_num = int(input("Invalid number. Put a 4-digit number:""))
File "/usr/lib/python3.5/unittest/mock.py", line 916, in __call__
_mock_self._mock_check_sig(*args, **kwargs)
KeyboardInterrupt
What can be wrong?