Sophie

Sophie

distrib > Fedora > 18 > i386 > by-pkgid > 7f671eb35339cf812de52087b0d93519 > files > 255

python3-pytest-2.3.5-3.fc18.noarch.rpm



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Good Integration Practises</title>
    
    <link rel="stylesheet" href="_static/sphinxdoc.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '',
        VERSION:     '2.3.4.1',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="top" title="None" href="index.html" />
    <link rel="up" title="Getting started basics" href="overview.html" />
    <link rel="next" title="Project examples" href="projects.html" />
    <link rel="prev" title="Usage and Invocations" href="usage.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="projects.html" title="Project examples"
             accesskey="N">next</a></li>
        <li class="right" >
          <a href="usage.html" title="Usage and Invocations"
             accesskey="P">previous</a> |</li>
        <li><a href="contents.html">pytest-2.3.4.1</a> &raquo;</li>
          <li><a href="overview.html" accesskey="U">Getting started basics</a> &raquo;</li>
 
<g:plusone></g:plusone>

      </ul>
    </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none">
    <form class="search" action="search.html" method="get">
      <input type="text" name="q" size="18" />
      <input type="submit" value="Search" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>

<h3>quicklinks</h3>
<div style="text-align: left; font-size: 100%; vertical-align: middle;">
<table>
<tr>
<td>
        <a href="index.html">home</a>
</td><td>
        <a href="contents.html">TOC/contents</a>
</td></tr><tr><td>
        <a href="getting-started.html">install</a>
</td><td>
        <a href="changelog.html">changelog</a>
</td></tr><tr><td>
        <a href="example/index.html">examples</a>
</td><td>
        <a href="customize.html">customize</a>
</td></tr><tr><td>
        <a href="https://bitbucket.org/hpk42/pytest/issues?status=new&status=open">issues[bb]</a>
</td><td>
        <a href="contact.html">contact</a>
</td></tr></table>
</div>

  <h3><a href="contents.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Good Integration Practises</a><ul>
<li><a class="reference internal" href="#work-with-virtual-environments">Work with virtual environments</a></li>
<li><a class="reference internal" href="#use-tox-and-continuous-integration-servers">Use tox and Continuous Integration servers</a></li>
<li><a class="reference internal" href="#create-a-py-test-standalone-script">Create a py.test standalone script</a></li>
<li><a class="reference internal" href="#integrating-with-distutils-python-setup-py-test">Integrating with distutils / <tt class="docutils literal"><span class="pre">python</span> <span class="pre">setup.py</span> <span class="pre">test</span></tt></a></li>
<li><a class="reference internal" href="#integration-with-setuptools-distribute-test-commands">Integration with setuptools/distribute test commands</a></li>
<li><a class="reference internal" href="#conventions-for-python-test-discovery">Conventions for Python test discovery</a></li>
<li><a class="reference internal" href="#choosing-a-test-layout-import-rules">Choosing a test layout / import rules</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="usage.html"
                        title="previous chapter">Usage and Invocations</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="projects.html"
                        title="next chapter">Project examples</a></p>
        </div>
      </div>

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="good-integration-practises">
<span id="goodpractises"></span><h1>Good Integration Practises<a class="headerlink" href="#good-integration-practises" title="Permalink to this headline">¶</a></h1>
<div class="section" id="work-with-virtual-environments">
<h2>Work with virtual environments<a class="headerlink" href="#work-with-virtual-environments" title="Permalink to this headline">¶</a></h2>
<p>We recommend to use <a class="reference external" href="http://pypi.python.org/pypi/virtualenv">virtualenv</a> environments and use <a class="reference external" href="http://pypi.python.org/pypi/distribute">easy_install</a>
(or <a class="reference external" href="http://pypi.python.org/pypi/pip">pip</a>) for installing your application dependencies as well as
the <tt class="docutils literal"><span class="pre">pytest</span></tt> package itself.  This way you will get a much more reproducible
environment.  A good tool to help you automate test runs against multiple
dependency configurations or Python interpreters is <a class="reference external" href="http://codespeak.net/tox">tox</a>.</p>
</div>
<div class="section" id="use-tox-and-continuous-integration-servers">
<h2>Use tox and Continuous Integration servers<a class="headerlink" href="#use-tox-and-continuous-integration-servers" title="Permalink to this headline">¶</a></h2>
<p>If you frequently release code to the public you
may want to look into <a class="reference external" href="http://codespeak.net/tox">tox</a>, the virtualenv test automation
tool and its <a class="reference external" href="http://codespeak.net/tox/example/pytest.html">pytest support</a>.
The basic idea is to generate a JUnitXML file through the <tt class="docutils literal"><span class="pre">--junitxml=PATH</span></tt> option and have a continuous integration server like <a class="reference external" href="http://jenkins-ci.org/">Jenkins</a> pick it up
and generate reports.</p>
</div>
<div class="section" id="create-a-py-test-standalone-script">
<span id="genscript-method"></span><span id="standalone"></span><h2>Create a py.test standalone script<a class="headerlink" href="#create-a-py-test-standalone-script" title="Permalink to this headline">¶</a></h2>
<p>If you are a maintainer or application developer and want others
to easily run tests you can generate a completely standalone &#8220;py.test&#8221;
script:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">py</span><span class="o">.</span><span class="n">test</span> <span class="o">--</span><span class="n">genscript</span><span class="o">=</span><span class="n">runtests</span><span class="o">.</span><span class="n">py</span>
</pre></div>
</div>
<p>generates a <tt class="docutils literal"><span class="pre">runtests.py</span></tt> script which is a fully functional basic
<tt class="docutils literal"><span class="pre">py.test</span></tt> script, running unchanged under Python2 and Python3.
You can tell people to download the script and then e.g.  run it like this:</p>
<div class="highlight-python"><pre>python runtests.py</pre>
</div>
</div>
<div class="section" id="integrating-with-distutils-python-setup-py-test">
<h2>Integrating with distutils / <tt class="docutils literal"><span class="pre">python</span> <span class="pre">setup.py</span> <span class="pre">test</span></tt><a class="headerlink" href="#integrating-with-distutils-python-setup-py-test" title="Permalink to this headline">¶</a></h2>
<p>You can integrate test runs into your distutils or
setuptools based project.  Use the <a class="reference internal" href="#genscript-method">genscript method</a>
to generate a standalone py.test script:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">py</span><span class="o">.</span><span class="n">test</span> <span class="o">--</span><span class="n">genscript</span><span class="o">=</span><span class="n">runtests</span><span class="o">.</span><span class="n">py</span>
</pre></div>
</div>
<p>and make this script part of your distribution and then add
this to your <tt class="docutils literal"><span class="pre">setup.py</span></tt> file:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">distutils.core</span> <span class="kn">import</span> <span class="n">setup</span><span class="p">,</span> <span class="n">Command</span>
<span class="c"># you can also import from setuptools</span>

<span class="k">class</span> <span class="nc">PyTest</span><span class="p">(</span><span class="n">Command</span><span class="p">):</span>
    <span class="n">user_options</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">def</span> <span class="nf">initialize_options</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">pass</span>
    <span class="k">def</span> <span class="nf">finalize_options</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">pass</span>
    <span class="k">def</span> <span class="nf">run</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="kn">import</span> <span class="nn">sys</span><span class="o">,</span><span class="nn">subprocess</span>
        <span class="n">errno</span> <span class="o">=</span> <span class="n">subprocess</span><span class="o">.</span><span class="n">call</span><span class="p">([</span><span class="n">sys</span><span class="o">.</span><span class="n">executable</span><span class="p">,</span> <span class="s">&#39;runtests.py&#39;</span><span class="p">])</span>
        <span class="k">raise</span> <span class="ne">SystemExit</span><span class="p">(</span><span class="n">errno</span><span class="p">)</span>
<span class="n">setup</span><span class="p">(</span>
    <span class="c">#...,</span>
    <span class="n">cmdclass</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;test&#39;</span><span class="p">:</span> <span class="n">PyTest</span><span class="p">},</span>
    <span class="c">#...,</span>
<span class="p">)</span>
</pre></div>
</div>
<p>If you now type:</p>
<div class="highlight-python"><pre>python setup.py test</pre>
</div>
<p>this will execute your tests using <tt class="docutils literal"><span class="pre">runtests.py</span></tt>. As this is a
standalone version of <tt class="docutils literal"><span class="pre">py.test</span></tt> no prior installation whatsoever is
required for calling the test command. You can also pass additional
arguments to the subprocess-calls such as your test directory or other
options.</p>
</div>
<div class="section" id="integration-with-setuptools-distribute-test-commands">
<h2>Integration with setuptools/distribute test commands<a class="headerlink" href="#integration-with-setuptools-distribute-test-commands" title="Permalink to this headline">¶</a></h2>
<p>Distribute/Setuptools support test requirements,
which means its really easy to extend its test command
to support running a pytest from test requirements:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">setuptools.command.test</span> <span class="kn">import</span> <span class="n">test</span> <span class="k">as</span> <span class="n">TestCommand</span>
<span class="kn">import</span> <span class="nn">sys</span>

<span class="k">class</span> <span class="nc">PyTest</span><span class="p">(</span><span class="n">TestCommand</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">finalize_options</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">TestCommand</span><span class="o">.</span><span class="n">finalize_options</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">test_args</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">test_suite</span> <span class="o">=</span> <span class="bp">True</span>
    <span class="k">def</span> <span class="nf">run_tests</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c">#import here, cause outside the eggs aren&#39;t loaded</span>
        <span class="kn">import</span> <span class="nn">pytest</span>
        <span class="n">errno</span> <span class="o">=</span> <span class="n">pytest</span><span class="o">.</span><span class="n">main</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">test_args</span><span class="p">)</span>
        <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="n">errno</span><span class="p">)</span>

<span class="n">setup</span><span class="p">(</span>
    <span class="c">#...,</span>
    <span class="n">tests_require</span><span class="o">=</span><span class="p">[</span><span class="s">&#39;pytest&#39;</span><span class="p">],</span>
    <span class="n">cmdclass</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;test&#39;</span><span class="p">:</span> <span class="n">PyTest</span><span class="p">},</span>
    <span class="p">)</span>
</pre></div>
</div>
<p>Now if you run:</p>
<div class="highlight-python"><pre>python setup.py test</pre>
</div>
<p>this will download py.test if needed and then run py.test
as you would expect it to.</p>
</div>
<div class="section" id="conventions-for-python-test-discovery">
<span id="python-test-discovery"></span><span id="test-discovery"></span><h2>Conventions for Python test discovery<a class="headerlink" href="#conventions-for-python-test-discovery" title="Permalink to this headline">¶</a></h2>
<p><tt class="docutils literal"><span class="pre">py.test</span></tt> implements the following standard test discovery:</p>
<ul class="simple">
<li>collection starts from the initial command line arguments
which may be directories, filenames or test ids.</li>
<li>recurse into directories, unless they match <a class="reference internal" href="customize.html#confval-norecursedirs"><tt class="xref std std-confval docutils literal"><span class="pre">norecursedirs</span></tt></a></li>
<li><tt class="docutils literal"><span class="pre">test_*.py</span></tt> or <tt class="docutils literal"><span class="pre">*_test.py</span></tt> files, imported by their <a class="reference internal" href="#package-name">package name</a>.</li>
<li><tt class="docutils literal"><span class="pre">Test</span></tt> prefixed test classes (without an <tt class="docutils literal"><span class="pre">__init__</span></tt> method)</li>
<li><tt class="docutils literal"><span class="pre">test_</span></tt> prefixed test functions or methods are test items</li>
</ul>
<p>For examples of how to customize your test discovery <a class="reference internal" href="example/pythoncollection.html"><em>Changing standard (Python) test discovery</em></a>.</p>
<p>Within Python modules, py.test also discovers tests using the standard
<a class="reference internal" href="unittest.html#unittest-testcase"><em>unittest.TestCase</em></a> subclassing technique.</p>
</div>
<div class="section" id="choosing-a-test-layout-import-rules">
<h2>Choosing a test layout / import rules<a class="headerlink" href="#choosing-a-test-layout-import-rules" title="Permalink to this headline">¶</a></h2>
<p>py.test supports common test layouts:</p>
<ul>
<li><p class="first">inlining test directories into your application package, useful if you want to
keep (unit) tests and actually tested code close together:</p>
<div class="highlight-python"><pre>mypkg/
    __init__.py
    appmodule.py
    ...
    test/
        test_app.py
        ...</pre>
</div>
</li>
<li><p class="first">putting tests into an extra directory outside your actual application
code, useful if you have many functional tests or want to keep
tests separate from actual application code:</p>
<div class="highlight-python"><pre>mypkg/
    __init__.py
    appmodule.py
tests/
    test_app.py
    ...</pre>
</div>
</li>
</ul>
<p>In both cases you usually need to make sure that <tt class="docutils literal"><span class="pre">mypkg</span></tt> is importable,
for example by using the setuptools <tt class="docutils literal"><span class="pre">python</span> <span class="pre">setup.py</span> <span class="pre">develop</span></tt> method.</p>
<p>You can run your tests by pointing to it:</p>
<div class="highlight-python"><pre>py.test tests/test_app.py       # for external test dirs
py.test mypkg/test/test_app.py  # for inlined test dirs
py.test mypkg                   # run tests in all below test directories
py.test                         # run all tests below current dir
...</pre>
</div>
<div class="admonition note" id="package-name">
<p class="first admonition-title">Note</p>
<p>If py.test finds a &#8220;a/b/test_module.py&#8221; test file while
recursing into the filesystem it determines the import name
as follows:</p>
<ul class="simple">
<li>find <tt class="docutils literal"><span class="pre">basedir</span></tt> &#8211; this is the first &#8220;upward&#8221; (towards the root)
directory not containing an <tt class="docutils literal"><span class="pre">__init__.py</span></tt>.  If both the <tt class="docutils literal"><span class="pre">a</span></tt>
and <tt class="docutils literal"><span class="pre">b</span></tt> directories contain an <tt class="docutils literal"><span class="pre">__init__.py</span></tt> the basedir will
be the parent dir of <tt class="docutils literal"><span class="pre">a</span></tt>.</li>
<li>perform <tt class="docutils literal"><span class="pre">sys.path.insert(0,</span> <span class="pre">basedir)</span></tt> to make the test module
importable under the fully qualified import name.</li>
<li><tt class="docutils literal"><span class="pre">import</span> <span class="pre">a.b.test_module</span></tt> where the path is determined
by converting path separators <tt class="docutils literal"><span class="pre">/</span></tt> into &#8221;.&#8221; characters.  This means
you must follow the convention of having directory and file
names map directly to the import names.</li>
</ul>
<p class="last">The reason for this somewhat evolved importing technique is
that in larger projects multiple test modules might import
from each other and thus deriving a canonical import name helps
to avoid surprises such as a test modules getting imported twice.</p>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="projects.html" title="Project examples"
             >next</a></li>
        <li class="right" >
          <a href="usage.html" title="Usage and Invocations"
             >previous</a> |</li>
        <li><a href="contents.html">pytest-2.3.4.1</a> &raquo;</li>
          <li><a href="overview.html" >Getting started basics</a> &raquo;</li>
 
<g:plusone></g:plusone>

      </ul>
    </div>

    <div class="footer">
        &copy; Copyright 2012, holger krekel.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
    </div>
<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-7597274-13']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>
<script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script>

  </body>
</html>