I've got a program that's built on functions that taks user inputs inside the functions and not parameters before the function: for example, say my function is
def my_function():
a = input("a: ")
b = input("b: ")
print(a+b)
and from what I understand thus far about unit testing a function like that is harder to unit test than a function that works for example like this:
def another_function(a,b):
return(a+b)
So how do I go about testing a function that looks like my_function, for example? It feels as if it would be easy to test manually by just entering incorrect inputs and checking for errors, but I have to write a test suite that tests all my functions automatically.
inputto provide whatever values you want, or setinput=inputin the function definition and manually inject something to replace the built-ininput(see e.g. this code I tested with the latter method).printin your case, as your functions don't return anything (or, much better, restructure them toreturnandprintelsewhere). You need to have a mockstdoutthen make sure that the text you were expecting got passed to it.printis a function, which makes life a bit easier than with Python 2.