Created
November 12, 2012 22:50
-
-
Save eloraburns/4062594 to your computer and use it in GitHub Desktop.
nose plugin for ipython notebook (early alpha), me and Greg Ward!
This file contains 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
{ | |
"metadata": { | |
"name": "nose experiment" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"import sys, os\n", | |
"import logging\n", | |
"import unittest\n", | |
"\n", | |
"log = logging.getLogger()\n", | |
"\n", | |
"from nose import core, loader\n", | |
"\n", | |
"#logging.basicConfig(level=logging.DEBUG)\n", | |
"\n", | |
"from types import ModuleType" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 9 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def adder(a, b):\n", | |
" return a + b\n", | |
"\n", | |
"def test_nothing():\n", | |
" assert 1+1 == 2\n", | |
"\n", | |
"def test_adder():\n", | |
" assert adder(3, 4) == 7\n", | |
" assert adder(-3, -5) == -8\n", | |
" \n", | |
"import random\n", | |
"def test_random_generated_tests():\n", | |
" for _ in range(random.randint(1, 100)):\n", | |
" yield lambda: None\n", | |
"\n", | |
"flake = False\n", | |
"def test_flake():\n", | |
" global flake\n", | |
" flake = not flake\n", | |
" assert flake" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 149 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"class MyProgram(core.TestProgram):\n", | |
" # XXX yuck: copy superclass runTests() so we can instantiate our own runner class;\n", | |
" # can't do it early because we don't have access to nose's config object.\n", | |
" def runTests(self):\n", | |
" self.testRunner = MyRunner(self.config)\n", | |
" # the rest is mostly duplicate code ;-(\n", | |
" plug_runner = self.config.plugins.prepareTestRunner(self.testRunner)\n", | |
" if plug_runner is not None:\n", | |
" self.testRunner = plug_runner\n", | |
" self.result = self.testRunner.run(self.test)\n", | |
" self.success = self.result.wasSuccessful()\n", | |
" return self.success\n", | |
"\n", | |
"class MyResult(unittest.TestResult):\n", | |
" def make_bar(self, tests, failing):\n", | |
" return '''<div>\n", | |
" <div style=\"background:red; width:%dpx\"> </div>\n", | |
" <div style=\"background:green; width:%dpx\"> </div>\n", | |
" </div>''' % (\n", | |
" failing * 10, (tests - failing) * 10)\n", | |
"\n", | |
" def make_table_of_tests(self, tests):\n", | |
" table = '<table>'\n", | |
" for test in tests:\n", | |
" table += '<tr><td>%s</td><td><pre><![CDATA[%s]]></pre></td>' % (\n", | |
" str(test[0]), test[1])\n", | |
" table += '</table>'\n", | |
" return table\n", | |
" \n", | |
" def _repr_html_(self):\n", | |
" if self.errors or self.failures:\n", | |
" not_successes = len(self.errors) + len(self.failures)\n", | |
" return self.make_bar(self.testsRun, not_successes) + \\\n", | |
" '''\n", | |
" <h2 style=\"color:red\">Errors</h2>%s\n", | |
" <h2 style=\"color:red\">Failures</h2>%s''' % (\n", | |
" self.make_table_of_tests(self.errors),\n", | |
" self.make_table_of_tests(self.failures))\n", | |
" else:\n", | |
" return self.make_bar(self.testsRun, 0) + '<div>%d/%d tests passed</div>' % (\n", | |
" self.testsRun, self.testsRun)\n", | |
"\n", | |
"class MyRunner(object):\n", | |
" def __init__(self, config):\n", | |
" self.config = config\n", | |
"\n", | |
" def run(self, test):\n", | |
" result = MyResult()\n", | |
" if hasattr(result, 'startTestRun'): # python 2.7\n", | |
" result.startTestRun()\n", | |
" test(result)\n", | |
" if hasattr(result, 'stopTestRun'):\n", | |
" result.stopTestRun()\n", | |
" self.config.plugins.finalize(result)\n", | |
" self.result = result\n", | |
" return result" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 140 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"test_module = ModuleType('test_module')\n", | |
"test_module.__dict__.update(get_ipython().user_ns)\n", | |
"\n", | |
"ldr = loader.TestLoader()\n", | |
"tests = ldr.loadTestsFromModule(test_module)\n", | |
"#print(\"discovered: %r\" % list(tests._tests))\n", | |
"\n", | |
"tprog = MyProgram(argv=['dummy'], exit=False, suite=tests)\n", | |
"tprog.result" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"html": [ | |
"<div>\n", | |
" <div style=\"background:red; width:10px\"> </div>\n", | |
" <div style=\"background:green; width:100px\"> </div>\n", | |
" </div>\n", | |
" <h2 style=\"color:red\">Errors</h2><table></table>\n", | |
" <h2 style=\"color:red\">Failures</h2><table><tr><td>__main__.test_flake</td><td><pre><![CDATA[Traceback (most recent call last):\n", | |
" File \"/Users/taavi/src/nose/nose/case.py\", line 197, in runTest\n", | |
" self.test(*self.arg)\n", | |
" File \"<ipython-input-70-7926d1daff1e>\", line 20, in test_flake\n", | |
" assert flake\n", | |
"AssertionError\n", | |
"]]></pre></td></table>" | |
], | |
"output_type": "pyout", | |
"prompt_number": 148, | |
"text": [ | |
"<__main__.MyResult run=11 errors=0 failures=1>" | |
] | |
} | |
], | |
"prompt_number": 148 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"tprog.result._repr_html_()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"ename": "TypeError", | |
"evalue": "unsupported operand type(s) for -: 'int' and 'str'", | |
"output_type": "pyerr", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<ipython-input-123-b5aa3fc18433>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtprog\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mresult\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_repr_html_\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[0;32m<ipython-input-117-e6b307a243fb>\u001b[0m in \u001b[0;36m_repr_html_\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 32\u001b[0m for e in self.failures))\n\u001b[1;32m 33\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 34\u001b[0;31m return self.make_bar(self.testsRun, 'green') + '<div>%d/%d tests passed</div>' % (\n\u001b[0m\u001b[1;32m 35\u001b[0m self.testsRun, self.testsRun)\n\u001b[1;32m 36\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", | |
"\u001b[0;32m<ipython-input-117-e6b307a243fb>\u001b[0m in \u001b[0;36mmake_bar\u001b[0;34m(self, tests, failing)\u001b[0m\n\u001b[1;32m 18\u001b[0m \u001b[0;34m<\u001b[0m\u001b[0mspan\u001b[0m \u001b[0mstyle\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m\"background:green; width:%dpx\"\u001b[0m\u001b[0;34m>\u001b[0m\u001b[0;34m&\u001b[0m\u001b[0mnbsp\u001b[0m\u001b[0;34m;\u001b[0m\u001b[0;34m<\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0mspan\u001b[0m\u001b[0;34m>\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 19\u001b[0m </div>''' % (\n\u001b[0;32m---> 20\u001b[0;31m failing * 10, (tests - failing) * 10)\n\u001b[0m\u001b[1;32m 21\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 22\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_repr_html_\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", | |
"\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for -: 'int' and 'str'" | |
] | |
} | |
], | |
"prompt_number": 123 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 8 | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment