Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unittest causing sys.exit()

No matter what I do sys.exit() is called by unittest, even the most trivial examples. I can't tell if my install is messed up or what is going on.

IDLE 1.2.2      ==== No Subprocess ====
>>> import unittest
>>> 
>>> class Test(unittest.TestCase):
        def testA(self):
            a = 1
            self.assertEqual(a,1)

>>> unittest.main()
option -n not recognized
Usage: idle.pyw [options] [test] [...]

Options:
  -h, --help       Show this message
  -v, --verbose    Verbose output
  -q, --quiet      Minimal output

 Examples:
   idle.pyw                               - run default set of tests
   idle.pyw MyTestSuite                   - run suite 'MyTestSuite'
   idle.pyw MyTestCase.testSomething      - run MyTestCase.testSomething
   idle.pyw MyTestCase                    - run all 'test*' test methods
                                           in MyTestCase

Traceback (most recent call last):
  File "<pyshell#7>", line 1, in <module>
    unittest.main()
  File "E:\Python25\lib\unittest.py", line 767, in __init__
    self.parseArgs(argv)
  File "E:\Python25\lib\unittest.py", line 796, in parseArgs
    self.usageExit(msg)
  File "E:\Python25\lib\unittest.py", line 773, in usageExit
    sys.exit(2)
SystemExit: 2
>>> 
like image 844
Brian Paden Avatar asked Sep 17 '08 03:09

Brian Paden


2 Answers

In new Python 2.7 release, unittest.main() has a new argument.

If 'exit' is set to False, sys.exit() is not called during the execution of unittest.main().

like image 81
dmeister Avatar answered Oct 15 '22 17:10

dmeister


Your example is exiting on my install too. I can make it execute the tests and stay within Python by changing

unittest.main()

to

unittest.TextTestRunner().run(unittest.TestLoader().loadTestsFromTestCase(Test))

More information is available here in the Python Library Reference.

like image 23
Joe Skora Avatar answered Oct 15 '22 17:10

Joe Skora