|
| 1 | +<html> |
| 2 | + <head> |
| 3 | + <title>nose: ErrorClass Plugins</title> |
| 4 | + <link rel="stylesheet" href="site.css" type="text/css"></link> |
| 5 | + </head> |
| 6 | + <body> |
| 7 | + |
| 8 | + <div id="menu"> |
| 9 | + <p>This document covers nose version <b>0.10.0a1</b></p> |
| 10 | + FIXME -- menu |
| 11 | + <p>Last update: <b>Thu Jun 21 13:14:42 2007</b></p> |
| 12 | + </div> |
| 13 | + |
| 14 | + <div id="main"> |
| 15 | + <h1>nose: ErrorClass Plugins</h1> |
| 16 | + |
| 17 | + <p>ErrorClass plugins provide an easy way to add support for custom |
| 18 | +handling of particular classes of exceptions.</p> |
| 19 | +<p>An ErrorClass plugin defines one or more ErrorClasses and how each is |
| 20 | +handled and reported on. Each error class is stored in a different |
| 21 | +attribute on the result, and reported separately. Each error class must |
| 22 | +indicate the exceptions that fall under that class, the label to use |
| 23 | +for reporting, and whether exceptions of the class should be |
| 24 | +considered as failures for the whole test run.</p> |
| 25 | +<p>ErrorClasses use a declarative syntax. Assign an ErrorClass to the |
| 26 | +attribute you wish to add to the result object, defining the |
| 27 | +exceptions, label and isfailure attributes. For example, to declare an |
| 28 | +ErrorClassPlugin that defines TodoErrors (and subclasses of TodoError) |
| 29 | +as an error class with the label 'TODO' that is considered a failure, |
| 30 | +do this:</p> |
| 31 | +<blockquote> |
| 32 | +<pre class="doctest-block"> |
| 33 | +>>> class Todo(Exception): |
| 34 | +... pass |
| 35 | +>>> class TodoError(ErrorClassPlugin): |
| 36 | +... todo = ErrorClass(Todo, label='TODO', isfailure=True) |
| 37 | +</pre> |
| 38 | +</blockquote> |
| 39 | +<p>The MetaErrorClass metaclass translates the ErrorClass declarations |
| 40 | +into the tuples used by the error handling and reporting functions in |
| 41 | +the result. This is an internal format and subject to change; you |
| 42 | +should always use the declarative syntax for attaching ErrorClasses to |
| 43 | +an ErrorClass plugin.</p> |
| 44 | +<blockquote> |
| 45 | +<pre class="doctest-block"> |
| 46 | +>>> TodoError.errorClasses # doctest: +ELLIPSIS |
| 47 | +((<class ...Todo...>, ('todo', 'TODO', True)),) |
| 48 | +</pre> |
| 49 | +</blockquote> |
| 50 | +<p>Let's see the plugin in action. First some boilerplate.</p> |
| 51 | +<blockquote> |
| 52 | +<pre class="doctest-block"> |
| 53 | +>>> import sys |
| 54 | +>>> import unittest |
| 55 | +>>> buf = unittest._WritelnDecorator(sys.stdout) |
| 56 | +</pre> |
| 57 | +</blockquote> |
| 58 | +<p>Now define a test case that raises a Todo.</p> |
| 59 | +<blockquote> |
| 60 | +<pre class="doctest-block"> |
| 61 | +>>> class TestTodo(unittest.TestCase): |
| 62 | +... def runTest(self): |
| 63 | +... raise Todo("I need to test something") |
| 64 | +>>> case = TestTodo() |
| 65 | +</pre> |
| 66 | +</blockquote> |
| 67 | +<p>Prepare the result using our plugin. Normally this happens during the |
| 68 | +course of test execution within nose.</p> |
| 69 | +<blockquote> |
| 70 | +<pre class="doctest-block"> |
| 71 | +>>> plugin = TodoError() |
| 72 | +>>> result = unittest._TextTestResult(stream=buf, |
| 73 | +... descriptions=0, verbosity=2) |
| 74 | +>>> plugin.prepareTestResult(result) |
| 75 | +</pre> |
| 76 | +</blockquote> |
| 77 | +<p>Now run the test. TODO is printed.</p> |
| 78 | +<blockquote> |
| 79 | +<pre class="doctest-block"> |
| 80 | +>>> case(result) # doctest: +ELLIPSIS |
| 81 | +runTest (....TestTodo) ... TODO |
| 82 | +</pre> |
| 83 | +</blockquote> |
| 84 | +<p>Errors and failures are empty, but todo has our test:</p> |
| 85 | +<blockquote> |
| 86 | +<pre class="doctest-block"> |
| 87 | +>>> result.errors |
| 88 | +[] |
| 89 | +>>> result.failures |
| 90 | +[] |
| 91 | +>>> result.todo # doctest: +ELLIPSIS |
| 92 | +[(<....TestTodo testMethod=runTest>, '...Todo: I need to test something\n')] |
| 93 | +>>> result.printErrors() # doctest: +ELLIPSIS |
| 94 | +<BLANKLINE> |
| 95 | +====================================================================== |
| 96 | +TODO: runTest (....TestTodo) |
| 97 | +---------------------------------------------------------------------- |
| 98 | +Traceback (most recent call last): |
| 99 | +... |
| 100 | +Todo: I need to test something |
| 101 | +<BLANKLINE> |
| 102 | +</pre> |
| 103 | +</blockquote> |
| 104 | +<p>Since we defined a Todo as a failure, the run was not successful.</p> |
| 105 | +<blockquote> |
| 106 | +<pre class="doctest-block"> |
| 107 | +>>> result.wasSuccessful() |
| 108 | +False |
| 109 | +</pre> |
| 110 | +</blockquote> |
| 111 | + |
| 112 | + |
| 113 | + </div> |
| 114 | + </body> |
| 115 | +</html> |
| 116 | + |
0 commit comments