mercredi 14 septembre 2016

Unit test a Python While loop

Trying to unit-test that input from a user is not an empty string. The code is simple enough, the unit test I'm having difficulty implementing. How would I go about unit-testing the following func?

def readString():
    counter = False
    while counter == False:
        s = raw_input("What is the input string? ")
        if len(s)>0:
            counter = True
        else:
           print("User must enter something into program.")
    return s

So far I've got the following to work without the while loop implmentation:

def test_ensure_the_users_enters_something(self):
    with mock.patch('__builtin__.raw_input', return_value=TEST_EMPTY_STRING): 
            s=readString()
            self.assertEqual(s,TEST_EMPTY_STRING)

Not sure how to mock multiple values and test inside a while loop. (Using Python 2.7).

Aucun commentaire:

Enregistrer un commentaire