I think you're searching for testing. Python testing
There are many ways to get automated standard input for your code, but the one with better results (although an uncommon practice between beginners) is unit testing. It works like this:
- You write down your code, and divide it in functions (although you can make it in a single function, but this should be avoided).
- You write an unit test that sends some premade arguments to your code.
- You run the test and check whereas something is wrong.
By the way, python itself has a unittest library that makes it easy to run this kind of test. But the functionality that you're looking for (getting things to input()
) requires a more advanced implementation of the mock
lib with help from the patch
function.
Let's get into the code:
App.py
def printName():
return input("What's your name man?")
Test.py
from unittest.mock import patch
import app
@patch('builtins.input', lambda *args: 'Nolan')
def test_print_name():
# The why we call args there is explained in the link bellow
answer = app.printName() # Will call buitins.input inside it, but it's patched so don't worry about it
assert answer == 'Nolan' # True, so no errors here
if __name__ == "__main__":
test_query_y()
This code is based on this answer
You're missing a lot of capabilities from the unittest lib by not using classes. I mean, you could use it as it is (although this may not be a good practice). So I recommend that you learn more about the lib :3