1

How can I write a unittest that will press a key at the right time so I can test the check method below and it doesn't wait forever for a key press.

class MyProject():
    def __init__(self):
        self.info = ""

    def check(self):
        # goes online to fetch data and saves it to self.info
        input("Press any key to continue...")

    def main_function(self):
        # this method does the main task
M3RS
  • 6,720
  • 6
  • 37
  • 47
  • 2
    Please provide your code. See how to ask a [good question](https://stackoverflow.com/help/how-to-ask) – Lescurel Sep 11 '17 at 07:30
  • 1
    You don't "unit test" entire scripts; you unit test individual pieces of the functionality (e.g., make sure that a function returns the correct output for certain inputs). Something that includes user interaction is usually not a good test candidate. You need to organize your code better. – Karl Knechtel Sep 11 '17 at 07:38
  • Thanks Karl, that's a very good point and it helped a lot. I will reorganise the code so the press any key bit is outside of the functions/methods. – M3RS Sep 11 '17 at 07:43
  • Consider https://stackoverflow.com/a/17353967/2482744 – Alex Hall Sep 11 '17 at 07:43
  • Possible duplicate of [How to supply stdin, files and environment variable inputs to Python unit tests?](https://stackoverflow.com/questions/2617057/how-to-supply-stdin-files-and-environment-variable-inputs-to-python-unit-tests) – Peter Wood Sep 11 '17 at 08:23

0 Answers0