Last active
July 26, 2021 13:42
-
-
Save vlad-bezden/799122343c12583ac8ffe9ad98234b9a to your computer and use it in GitHub Desktop.
Example of how to use unittest in IPython or Jupyter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Example of how to use unittest in IPython or Jupyter\n", | |
"\n", | |
"Configuration for runnint unittest in IPython or Jupyter is different than running unittest from command line. \n", | |
"\n", | |
"When you run following code from IPython or Jupyter\n", | |
"\n", | |
"```python\n", | |
"if __name__ == '__main__':\n", | |
" unittest.main()\n", | |
"```\n", | |
"\n", | |
"will result in following error:\n", | |
"\n", | |
"```\n", | |
"E\n", | |
"======================================================================\n", | |
"ERROR: C:\\Users\\XXXX\\AppData\\Roaming\\jupyter\\runtime\\kernel-4b12e789-a050-4567-a8a1-7361d4a1745f (unittest.loader._FailedTest)\n", | |
"----------------------------------------------------------------------\n", | |
"AttributeError: module '__main__' has no attribute 'C:\\Users\\XXXX\\AppData\\Roaming\\jupyter\\runtime\\kernel-4b12e789-a050-4567-a8a1-7361d4a1745f'\n", | |
"\n", | |
"----------------------------------------------------------------------\n", | |
"Ran 1 test in 0.001s\n", | |
"\n", | |
"FAILED (errors=1)\n", | |
"An exception has occurred, use %tb to see the full traceback.\n", | |
"\n", | |
"SystemExit: True\n", | |
"\n", | |
"\n", | |
"d:\\development\\lib\\site-packages\\IPython\\core\\interactiveshell.py:2855: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.\n", | |
" warn(\"To exit: use 'exit', 'quit', or Ctrl-D.\", stacklevel=1)\n", | |
"```\n", | |
"\n", | |
"The reason is that `unittest.main` looks at `sys.argv` and first parameter is what started IPython or Jupyter, therefore the error about kernel connection file not being a valid attribute.\n", | |
"Passing explicit list to `unittest.main` will prevent IPython and Jupyter look at `sys.argv`.\n", | |
"Passing `exit=Fals` will prevent `unittest.main` to shutdown the kernell process\n", | |
"\n", | |
"```python\n", | |
"if __name__ == '__main__':\n", | |
" unittest.main(argv=['first-arg-is-ignored'], exit=False)\n", | |
"```" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import unittest\n", | |
"\n", | |
"class TestDemo(unittest.TestCase):\n", | |
" \"\"\"Example of how to use unittest in Jupyter.\"\"\"\n", | |
" \n", | |
" def test(self):\n", | |
" self.assertEqual('foo'.upper(), 'FOO') " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
".\n", | |
"----------------------------------------------------------------------\n", | |
"Ran 1 test in 0.005s\n", | |
"\n", | |
"OK\n" | |
] | |
} | |
], | |
"source": [ | |
"if __name__ == '__main__':\n", | |
" unittest.main(argv=['first-arg-is-ignored'], exit=False)" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.6.1" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment