Domanda

I have a python script containing a unittest.TestCase, with a setUp() function and a small number of test_foo_does_bar()-type functions.

The script ends as follows:

if __name__ == '__main__':
    unittest.main()

When I run this script in Spyder (see below for config details) the interpreter stops at the following line:

----------------------------------------------------------------------
Ran x tests in x.xxxs

FAILED (failures=x)

No amount of Ctrl+C or Ctrl+D can rescue the interpreter and get me back to the prompt. The same script, run from the command prompt, terminates as normal.

Is this a bug in Spyder or am I missing something?

Setup info: Spyder 2.2.3 Python 2.7.5 64bits. Qt 4.8.4, PyQt4 (API v2) 4.9.6 on Windows

È stato utile?

Soluzione

It doesn't crash the Python interpreter, it terminates it. This is normal behavior for the script.
You can see it yourself: in main.py it is stated, main = TestProgram; this means that unittest.main() will call the TestProgram class, which in its __init__(self) calls self.runTests(), which ends with this:

if self.exit:
    sys.exit(not self.result.wasSuccessful())

So it has a specific call to sys.exit(), which terminates the interpreter.

In fact, if you run it as command line, you return to the command line; and if you start command-line Python, import the module and call the function, you will see that you'll return to the command prompt - you don't stay in Python.

If you want the script to NOT terminate the interpreter, just state it when calling the funcion:

unittest.main(exit=False)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top