Search code examples
pythonnosenosetestsraw-input

nose test freezing at raw_input


I have a nose test that imports a file which runs a class with raw_inputs. Whenever I type nosetests in the command line, the prompt simply pauses and doesn't continue - I have to keyboard interrupt to see what happens, and it turns out the nose test is running my file up to the first raw_input (one of many), at which point it simply pauses and cannot continue.

Any way to bypass this? Thanks!


Solution

  • If possible, rewrite the file so it won't call raw_input() when imported.

    # imported file
    if __name__ == "__main__":
        raw_input()
    

    Otherwise, if you can figure out in advance what would be acceptable input, you can take standard input from a file. Assuming input.txt contains "Pass":

    nosetests test_input.py < input.txt
    

    where test_input.py is:

    # test file
    def test_input():
        s = raw_input()
        assert s.strip() == "Pass"
    

    Or you can pipe acceptable input into nosetests:

    c:\>echo Pass | nosetests test_input.py
    .
    ----------------------------------------------------------------------
    Ran 1 test in 0.001s
    OK
    
    c:\>echo Fail | nosetests test_input.py
    F
    ======================================================================
    FAIL: cgp.test.test_input.test_input
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "C:\Python27\lib\site-packages\nose\case.py", line 187, in runTest
        self.test(*self.arg)
      File "c:\test_input.py", line 3, in test_input
        assert s.strip() == "Pass"
    AssertionError
    ----------------------------------------------------------------------
    Ran 1 test in 0.002s
    FAILED (failures=1)