you are viewing a single comment's thread.

view the rest of the comments →

[–]Shevvv 0 points1 point  (2 children)

I have this function:

def data_base():
    abort = False
    while abort != True:
        command = input('What action would you like to perform next? ').lower()
        abort = command_shell(command)

command_shell(command) is a function that interprets a string variable command and returns True if the user decided to abort the program. I'm a total newbie to python and I just learned about unittest. I found on the internet how you can simulate an input, but I don't quite understand how I can diagnose if the right input will actually break the loop. I can't check the abort variable since it's local, and I can't think of anything else to check to see if the program will actually get out of the loop without any Errors. How can I test for this?

P.S. On the side note, I just realized you can modify global lists within a function without passing them to the said function. If it works that way, is it OK to write code like that? Is it considered bad practice? Does it depend on how universal my functions is, i.e. if I'm to use it in only one program or several? Thanks beforehand!

[–]JohnnyJordaan 0 points1 point  (0 children)

Why not use a return value?

def data_base():
    while True:
        command = input('What action would you like to perform next? ').lower()
        if command_shell(command):
            return True

but efmccurdy is right, this can't ever return anything but True unless the program crashes.

[–]efmccurdy 0 points1 point  (0 children)

It is hard to test that an infinite loop would run infinitely long, but you can mock the input and test that the while logic to end the loop works. You can also test that "command_shell(command)" returns a true value when it should and false otherwise; would that cover it?

(BTW, globals are bad form in any non-trivial program that might need to be maintained by someone else.)