0

I am trying to streamline my efficiency working with VSCode.

I'd like to be able to run a script which includes input() commands in the Terminal, and when it runs to be able to immediately begin interacting with the program.

As it is, I have to run the program, then click over to interact with it.

Is there any way to make this happen? Jupyter doesn't seem to do the trick..

ninthtale
  • 3
  • 3

1 Answers1

0

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:

  1. You write down your code, and divide it in functions (although you can make it in a single function, but this should be avoided).
  2. You write an unit test that sends some premade arguments to your code.
  3. 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