ipython / rlipython

Readline Interface for IPython 5.4+
Other
32 stars 10 forks source link

error running unittest #17

Open yamad opened 7 years ago

yamad commented 7 years ago

Running tests using unittest from the standard library gives an error about unrecognized arguments. I know this is supposed to be an unmaintained module, but I wonder if IPython already has a standard way of hiding ipython specific args.

Maybe a little unusual to actually want to run unittest inside a repl, but the standard repl handles this without error.

A minimal testcase shows the issue:

$ ipython --TerminalIPythonApp.interactive_shell_class=rlipython.TerminalInteractiveShell
Python 3.6.1 (default, Apr  4 2017, 09:40:51)
Type 'copyright', 'credits' or 'license' for more information
IPython 6.0.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import unittest

In [2]: class IpythonTest(unittest.TestCase):
   ...:     def test_test(self):
   ...:         self.assertTrue(True)
   ...:

In [3]: unittest.main()
usage: ipython [-h] [-v] [-q] [--locals] [-f] [-c] [-b] [tests [tests ...]]
ipython: error: unrecognized arguments: --TerminalIPythonApp.interactive_shell_class=rlipython.TerminalInteractiveShell
An exception has occurred, use %tb to see the full traceback.

SystemExit: 2

/usr/local/lib/python3.6/site-packages/IPython/core/interactiveshell.py:2855: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
  warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)

In [4]: %tb
---------------------------------------------------------------------------
SystemExit                                Traceback (most recent call last)
<ipython-input-3-7d361a096586> in <module>()
----> 1 unittest.main()

/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/main.py in __init__(
self, module, defaultTest, argv, testRunner, testLoader, exit, verbosity, failfast, catchbreak, buffer, warnings, tb
_locals)
     91         self.testLoader = testLoader
     92         self.progName = os.path.basename(argv[0])
---> 93         self.parseArgs(argv)
     94         self.runTests()
     95

/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/main.py in parseArg$
(self, argv)
    124                 return
    125         else:
--> 126             self._main_parser.parse_args(argv[1:], self)
    127
    128         if self.tests:

/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/argparse.py in parse_args(se$
f, args, namespace)
   1731         if argv:
   1732             msg = _('unrecognized arguments: %s')
-> 1733             self.error(msg % ' '.join(argv))
   1734         return args
   1735

/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/argparse.py in error(self, m$
ssage)
   2387         self.print_usage(_sys.stderr)
   2388         args = {'prog': self.prog, 'message': message}
-> 2389         self.exit(2, _('%(prog)s: error: %(message)s\n') % args)

/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/argparse.py in exit(self, st$
tus, message)
   2374         if message:
   2375             self._print_message(message, _sys.stderr)
-> 2376         _sys.exit(status)
   2377
   2378     def error(self, message):

SystemExit: 2

For reference, on vanilla IPython, the tests actually run but some noise complains at the end too:

$ ipython
Python 3.6.1 (default, Apr  4 2017, 09:40:51)
Type 'copyright', 'credits' or 'license' for more information
IPython 6.0.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import unittest

In [2]: class IpythonTest(unittest.TestCase):
   ...:     def test_test(self):
   ...:         self.assertTrue(True)
   ...:

In [3]: unittest.main()
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
An exception has occurred, use %tb to see the full traceback.

SystemExit: False

/usr/local/lib/python3.6/site-packages/IPython/core/interactiveshell.py:2855: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
  warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)
kmcminn commented 5 years ago

this issue is a tad old I realize, but this is because sys.argv is already being populated by ipython's args. Use:

unittest.main(__name__, argv=['main'], exit=False)