Source

nose-pycon / doc / module_nose.twistedtools.html

<html>
  <head>
    <title>nose: nose.twistedtools</title>
    <link rel="stylesheet" href="site.css" type="text/css"></link>
  </head>
  <body>
    
    <div id="menu">
      <p>This document covers nose version <b>0.10.1</b></p>
      <p>Last update: <b>Fri Jan  4 10:06:23 2008</b></p>
      <h2>Plugins</h2><ul><li><a href="plugin_attrib.html">Builtin Plugin: attrib</a></li><li><a href="plugin_capture.html">Builtin Plugin: capture</a></li><li><a href="plugin_cover.html">Builtin Plugin: cover</a></li><li><a href="plugin_debug.html">Builtin Plugin: debug</a></li><li><a href="plugin_deprecated.html">Builtin Plugin: deprecated</a></li><li><a href="plugin_doctests.html">Builtin Plugin: doctests</a></li><li><a href="plugin_failuredetail.html">Builtin Plugin: failuredetail</a></li><li><a href="plugin_isolate.html">Builtin Plugin: isolate</a></li><li><a href="plugin_prof.html">Builtin Plugin: prof</a></li><li><a href="plugin_skip.html">Builtin Plugin: skip</a></li><li><a href="plugin_testid.html">Builtin Plugin: testid</a></li><li><a href="error_class_plugin.html">ErrorClass Plugins</a></li><li><a href="plugin_interface.html">Plugin Interface</a></li><li><a href="writing_plugins.html">Writing Plugins</a></li></ul><h2>Modules</h2><ul><li><a href="module_nose.case.html">Module: nose.case</a></li><li><a href="module_nose.commands.html">Module: nose.commands</a></li><li><a href="module_nose.config.html">Module: nose.config</a></li><li><a href="module_nose.core.html">Module: nose.core</a></li><li><a href="module_nose.exc.html">Module: nose.exc</a></li><li><a href="module_nose.importer.html">Module: nose.importer</a></li><li><a href="module_nose.inspector.html">Module: nose.inspector</a></li><li><a href="module_nose.loader.html">Module: nose.loader</a></li><li><a href="module_nose.plugins.manager.html">Module: nose.plugins.manager</a></li><li><a href="module_nose.plugins.plugintest.html">Module: nose.plugins.plugintest</a></li><li><a href="module_nose.proxy.html">Module: nose.proxy</a></li><li><a href="module_nose.result.html">Module: nose.result</a></li><li><a href="module_nose.selector.html">Module: nose.selector</a></li><li><a href="module_nose.suite.html">Module: nose.suite</a></li><li><a href="module_nose.tools.html">Module: nose.tools</a></li><li><a href="module_nose.twistedtools.html">Module: nose.twistedtools</a></li><li><a href="module_nose.util.html">Module: nose.util</a></li></ul><h2>Plugin Examples</h2><ul><li><a href="unwanted_package.html">Excluding Unwanted Packages</a></li><li><a href="empty_plugin.html">Minimal plugin</a></li><li><a href="init_plugin.html">Running Initialization Code Before the Test Run</a></li><li><a href="selector_plugin.html">Using a Custom Selector</a></li><li><a href="plugin_exceptions.html">When Plugins Fail</a></li><li><a href="plugintest_environment.html">nose.plugins.plugintest, os.environ and sys.argv</a></li></ul>
      <h2>Functions</h2><ul><li><a href="#deferred">deferred</a></li><li><a href="#stop_reactor">stop_reactor</a></li><li><a href="#threaded_reactor">threaded_reactor</a></li></ul><h2>Attributes</h2><ul><li><a href="#reactor">reactor</a></li></ul>
    </div>
    
    <div id="main">
      <h1>nose: nose.twistedtools</h1>
      
      <p>This module provides a very simple way to integrate your tests with the
Twisted event loop.</p>
<p>You must import this module <em>before</em> importing anything from Twisted itself!</p>
<p>Example:</p>
<pre class="literal-block">
from nose.twistedtools import reactor, deferred

&#64;deferred()
def test_resolve():
    return reactor.resolve(&quot;www.python.org&quot;)
</pre>
<p>Or, more realistically:</p>
<pre class="literal-block">
&#64;deferred(timeout=5.0)
def test_resolve():
    d = reactor.resolve(&quot;www.python.org&quot;)
    def check_ip(ip):
        assert ip == &quot;67.15.36.43&quot;
    d.addCallback(check_ip)
    return d
</pre>
<h2>Functions</h2>
<a name="deferred"></a><div class="func section"><span class="func name">deferred<span class="args">(timeout=None)</span></span><div class="func doc"><p>By wrapping a test function with this decorator, you can return a
twisted Deferred and the test will wait for the deferred to be triggered.
The whole test function will run inside the Twisted event loop.</p>
<p>The optional timeout parameter specifies the maximum duration of the test.
The difference with timed() is that timed() will still wait for the test
to end, while deferred() will stop the test when its timeout has expired.
The latter is more desireable when dealing with network tests, because
the result may actually never arrive.</p>
<p>If the callback is triggered, the test has passed.
If the errback is triggered or the timeout expires, the test has failed.</p>
<p>Example:</p>
<pre class="literal-block">
&#64;deferred(timeout=5.0)
def test_resolve():
    return reactor.resolve(&quot;nose.python-hosting.com&quot;)
</pre>
<p>Attention! If you combine this decorator with other decorators (like
&quot;raises&quot;), deferred() must be called <em>first</em>!</p>
<p>In other words, this is good:</p>
<pre class="literal-block">
&#64;raises(DNSLookupError)
&#64;deferred()
def test_error():
    return reactor.resolve(&quot;xxxjhjhj.biz&quot;)
</pre>
<p>and this is bad:</p>
<pre class="literal-block">
&#64;deferred()
&#64;raises(DNSLookupError)
def test_error():
    return reactor.resolve(&quot;xxxjhjhj.biz&quot;)
</pre>
</div></div>
<a name="stop_reactor"></a><div class="func section"><span class="func name">stop_reactor<span class="args">()</span></span><div class="func doc"><p>Stop the reactor and join the reactor thread until it stops.
Call this function in teardown at the module or package level to
reset the twisted system after your tests. You <em>must</em> do this if
you mix tests using these tools and tests using twisted.trial.</p>
</div></div>
<a name="threaded_reactor"></a><div class="func section"><span class="func name">threaded_reactor<span class="args">()</span></span><div class="func doc"><p>Start the Twisted reactor in a separate thread, if not already done.
Returns the reactor.
The thread will automatically be destroyed when all the tests are done.</p>
</div></div><h2>Attributes</h2>
<a name="reactor"></a><div class="attr section"><span class="attr name">reactor</span><pre class="attr value">Default value: &lt;twisted.internet.selectreactor.SelectReactor object&gt;</pre><div class="attr doc"></div></div>

    </div>
    <script src="http://www.google-analytics.com/urchin.js" 
	    type="text/javascript">
    </script>
    <script type="text/javascript">
      _uacct = "UA-2236166-1";
      urchinTracker();
    </script>
  </body>
</html>