Coming soon - Get a detailed view of why an account is flagged as spam!
view details

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.

3
How to unit test a function containing user input?
Post Body

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?

Author
Account Strength
100%
Account Age
14 years
Verified Email
Yes
Verified Flair
No
Total Karma
21,887
Link Karma
6,444
Comment Karma
15,423
Profile updated: 2 days ago
Posts updated: 10 months ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
7 years ago