It might be worth looking back at one of the seminars last week as Zed covered something very similar.
There are two approaches to this; automation the input action via a tool that interacts like a human, or mock the input (i.e. hard code a value to ensure it gets passed every time). I don’t think you want to do the human interaction part, as that is usually UI testing with tools like Selenium/Appium, but mocking it nice and easy.
There are several ways to do this and below are a couple of resources (the keyword is ‘mock’ when searching):
import mock
def yes_or_no():
answer = input("Do you want to quit? > ")
if answer == "yes":
return("Quitter!")
elif answer == "no":
return("Awesome!")
else:
return("BANG!")
def test_quitting():
with mock.patch('builtins.input', return_value="yes"):
assert yes_or_no() == "Quitter!"
with mock.patch('builtins.input', return_value="no"):
assert yes_or_no() == "Awesome!"
The print statement is a real PITA so I change it to return, otherwise the output is None. I got totally trapped by the print / return thing for a while as I assume they are the same. They are not.
Thanks @io_io and @gpkesley. It feels good to have some fellows. I will try your suggestions and will come back to tell you about my success with it or ask more questions.
I saw zed doing it with my code but didn’t understand what he did exactly
Totally agree. And the good thing about this forum over some others is that we are all reasonably aware of the content each other are studying, and have similar challenges.
Something like stackoverflow is great for getting solutions to difficult problems, but often the responses can be very complicated or require pro-level understanding. Here, when someone say they can’t do something, we can all offer support by recalling what we did when we hit the same blocker.
Yes, actually remember I had to figure that out also in Refactor School S5? I had to figure out how to mock out and fake out the input function in that video so definitely watch that.