Skip to content

Instantly share code, notes, and snippets.

@tonyfast
Created January 27, 2018 19:45
Show Gist options
  • Save tonyfast/6bad41e5b6f069a61b1664ee7d479bba to your computer and use it in GitHub Desktop.
Save tonyfast/6bad41e5b6f069a61b1664ee7d479bba to your computer and use it in GitHub Desktop.
Use unittest in a notebook instance
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"def func():\n",
" \"\"\"\n",
" >>> 10\n",
" 10\n",
" >>> assert True\n",
" \"\"\""
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"__test__ = dict(\n",
" a=\"\"\"\n",
" A test tests below\n",
" \n",
" >>> print(42)\n",
" 42\n",
" \"\"\"\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"TestResults(failed=0, attempted=3)"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import doctest\n",
"\n",
"doctest.testmod()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"from unittest import TestSuite, TestResult, TestCase"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"class tester(TestCase):\n",
" def runTest(tester):\n",
" assert True"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"\n",
"suite = TestSuite([\n",
" tester(), doctest.DocTestSuite(__import__(f\"{__name__}\"))\n",
"])"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<unittest.result.TestResult run=3 errors=0 failures=0>"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"suite.run(TestResult())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "p6",
"language": "python",
"name": "other-env"
},
"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.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment