Suppose I have a function like this:
import random
def randomfunc():
x = random.randint(0,10)
# some code here which generates a name/text randomly and stores it in a .txt file locally/cloud
if x%2 == 0:
y=input("Enter your name:") # I need to enter that name/text exact here
print("Your name is ",y)
else:
print("You got an odd number")
Now, whenever I run this function then I want it to detect if it is asking for any input or not. If it's asking for input then enter the input else do nothing.
Remember I can't change anything inside the function.
I tried this:
import builtins
from unittest.mock import patch
with patch('builtins.input') as input_mock:
input_mock.side_effect = [
'My Name',
]
print(input('Enter your name:'))
But the problem here is that the input is predetermined before the execution of the input statement. In my case, my input value will be decided while the execution of randomfunc() as my input value is dynamic/it changes with time.
Also, when using wexpect module, it gives me OSError: [WinError 6] The handle is invalid.
Please suggest me some solution.