This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
Hello folks, I have a function that expects a user's input to which I'd like to write a unit test using pytest:
def agreeToLogin():
'''Asks if the user wants to login to or not.
@return: true if yes to login, false if no
'''
while True:
answer = input("Do you wish to login first? (y/n): ")
if answer == "":
print("Please input either y for yes, or n for no.")
continue
if answer == "n":
return False
print("Login will be skipped.")
break
if answer == "y":
return True
print("Login will not be skipped.")
break
else:
continue
The function does not take any arguments and thus I can't just simply use an assert in the test. Also, the test returns this when called: https://pastebin.com/BSqQeHj0
I tried some relevant, I hope, solutions described here: https://stackoverflow.com/questions/35851323/pytest-how-to-test-a-function-with-input-call and I also played a bit with: https://docs.pytest.org/en/latest/monkeypatch.html both to no success.
How would I write a unit test for a function like this? Do I need to re-write the function itself so it's easier to test?
Subreddit
Post Details
- Posted
- 7 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/learnpython...