1. Hai-Anh Trinh
  2. nose

Source

nose / doc / plugin_capture.html

<html>
  <head>
    <title>nose: builtin plugin: capture</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>
    </div>
    
    <div id="main">
      <h1>nose: builtin plugin: capture</h1>
      
      <p>This plugin captures stdout during test execution, appending any
output captured to the error or failure output, should the test fail
or raise an error. It is enabled by default but may be disable with
the options -s or --nocapture.</p>


      <h2>Plugin Methods Implemented</h2>

      <p>This plugin implements the following plugin interface methods:</p>
      
      <ul><li><a href="plugin_interface.html#afterTest">afterTest</a></li><li><a href="plugin_interface.html#beforeTest">beforeTest</a></li><li><a href="plugin_interface.html#begin">begin</a></li><li><a href="plugin_interface.html#configure">configure</a></li><li><a href="plugin_interface.html#finalize">finalize</a></li><li><a href="plugin_interface.html#formatError">formatError</a></li><li><a href="plugin_interface.html#formatFailure">formatFailure</a></li><li><a href="plugin_interface.html#options">options</a></li></ul>


      <h2>Commandline Options</h2>

      <p>This plugin adds the following commandline options:</p>

      <pre>Options:
  -s, --nocapture  Don't capture stdout (any stdout output will be printed
                   immediately) [NOSE_NOCAPTURE]
</pre>

      <h2>Source</h2>

      <div class="highlight"><pre><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">This plugin captures stdout during test execution, appending any</span>
<span class="sd">output captured to the error or failure output, should the test fail</span>
<span class="sd">or raise an error. It is enabled by default but may be disable with</span>
<span class="sd">the options -s or --nocapture.</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="k">import</span> <span class="nn">logging</span>
<span class="k">import</span> <span class="nn">os</span>
<span class="k">import</span> <span class="nn">sys</span>
<span class="k">from</span> <span class="nn">nose.plugins.base</span> <span class="k">import</span> <span class="n">Plugin</span>
<span class="k">from</span> <span class="nn">nose.util</span> <span class="k">import</span> <span class="n">ln</span>
<span class="k">try</span><span class="p">:</span>
    <span class="k">from</span> <span class="nn">cStringIO</span> <span class="k">import</span> <span class="n">StringIO</span>
<span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
    <span class="k">from</span> <span class="nn">StringIO</span> <span class="k">import</span> <span class="n">StringIO</span>


<span class="n">log</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="n">__name__</span><span class="p">)</span>

<span class="k">class</span> <span class="nc">Capture</span><span class="p">(</span><span class="n">Plugin</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Output capture plugin. Enabled by default. Disable with -s or</span>
<span class="sd">    --nocapture. This plugin captures stdout during test execution,</span>
<span class="sd">    appending any output captured to the error or failure output,</span>
<span class="sd">    should the test fail or raise an error.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">enabled</span> <span class="o">=</span> <span class="bp">True</span>
    <span class="n">env_opt</span> <span class="o">=</span> <span class="s">&#39;NOSE_NOCAPTURE&#39;</span>
    <span class="n">name</span> <span class="o">=</span> <span class="s">&#39;capture&#39;</span>
    <span class="n">score</span> <span class="o">=</span> <span class="mi">500</span>
    
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">stdout</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_buf</span> <span class="o">=</span> <span class="bp">None</span>

    <span class="k">def</span> <span class="nf">options</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">parser</span><span class="p">,</span> <span class="n">env</span><span class="o">=</span><span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">):</span>
        <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span>
            <span class="s">&quot;-s&quot;</span><span class="p">,</span> <span class="s">&quot;--nocapture&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&quot;store_false&quot;</span><span class="p">,</span>
            <span class="n">default</span><span class="o">=</span><span class="ow">not</span> <span class="n">env</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">env_opt</span><span class="p">),</span> <span class="n">dest</span><span class="o">=</span><span class="s">&quot;capture&quot;</span><span class="p">,</span>
            <span class="n">help</span><span class="o">=</span><span class="s">&quot;Don&#39;t capture stdout (any stdout output &quot;</span>
            <span class="s">&quot;will be printed immediately) [NOSE_NOCAPTURE]&quot;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">configure</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">options</span><span class="p">,</span> <span class="n">conf</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">conf</span> <span class="o">=</span> <span class="n">conf</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">options</span><span class="o">.</span><span class="n">capture</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">enabled</span> <span class="o">=</span> <span class="bp">False</span>

    <span class="k">def</span> <span class="nf">afterTest</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">test</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">end</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_buf</span> <span class="o">=</span> <span class="bp">None</span>
        
    <span class="k">def</span> <span class="nf">begin</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">start</span><span class="p">()</span> <span class="c"># get an early handle on sys.stdout</span>

    <span class="k">def</span> <span class="nf">beforeTest</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">test</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
        
    <span class="k">def</span> <span class="nf">formatError</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">test</span><span class="p">,</span> <span class="n">err</span><span class="p">):</span>
        <span class="n">test</span><span class="o">.</span><span class="n">capturedOutput</span> <span class="o">=</span> <span class="n">output</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">buffer</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_buf</span> <span class="o">=</span> <span class="bp">None</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">output</span><span class="p">:</span>
            <span class="c"># Don&#39;t return None as that will prevent other</span>
            <span class="c"># formatters from formatting and remove earlier formatters</span>
            <span class="c"># formats, instead return the err we got</span>
            <span class="k">return</span> <span class="n">err</span> 
        <span class="n">ec</span><span class="p">,</span> <span class="n">ev</span><span class="p">,</span> <span class="n">tb</span> <span class="o">=</span> <span class="n">err</span>
        <span class="k">return</span> <span class="p">(</span><span class="n">ec</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">addCaptureToErr</span><span class="p">(</span><span class="n">ev</span><span class="p">,</span> <span class="n">output</span><span class="p">),</span> <span class="n">tb</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">formatFailure</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">test</span><span class="p">,</span> <span class="n">err</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">formatError</span><span class="p">(</span><span class="n">test</span><span class="p">,</span> <span class="n">err</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">addCaptureToErr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ev</span><span class="p">,</span> <span class="n">output</span><span class="p">):</span>
        <span class="k">return</span> <span class="s">&#39;</span><span class="se">\n</span><span class="s">&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="nb">str</span><span class="p">(</span><span class="n">ev</span><span class="p">)</span> <span class="p">,</span> <span class="n">ln</span><span class="p">(</span><span class="s">&#39;&gt;&gt; begin captured stdout &lt;&lt;&#39;</span><span class="p">),</span>
                          <span class="n">output</span><span class="p">,</span> <span class="n">ln</span><span class="p">(</span><span class="s">&#39;&gt;&gt; end captured stdout &lt;&lt;&#39;</span><span class="p">)])</span>

    <span class="k">def</span> <span class="nf">start</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_buf</span> <span class="o">=</span> <span class="n">StringIO</span><span class="p">()</span>
        <span class="n">sys</span><span class="o">.</span><span class="n">stdout</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_buf</span>

    <span class="k">def</span> <span class="nf">end</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">stdout</span><span class="p">:</span>
            <span class="n">sys</span><span class="o">.</span><span class="n">stdout</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">finalize</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">result</span><span class="p">):</span>
        <span class="k">while</span> <span class="bp">self</span><span class="o">.</span><span class="n">stdout</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">end</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">_get_buffer</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_buf</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_buf</span><span class="o">.</span><span class="n">getvalue</span><span class="p">()</span>

    <span class="nb">buffer</span> <span class="o">=</span> <span class="nb">property</span><span class="p">(</span><span class="n">_get_buffer</span><span class="p">,</span> <span class="bp">None</span><span class="p">,</span> <span class="bp">None</span><span class="p">,</span>
                      <span class="sd">&quot;&quot;&quot;Captured stdout output.&quot;&quot;&quot;</span><span class="p">)</span>
</pre></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>