Sophie

Sophie

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

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>Changing standard (Python) test discovery</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="Usages and Examples" href="index.html" />
    <link rel="next" title="Working with non-python tests" href="nonpython.html" />
    <link rel="prev" title="A sesssion-fixture which can look at all collected tests" href="special.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="nonpython.html" title="Working with non-python tests"
             accesskey="N">next</a></li>
        <li class="right" >
          <a href="special.html" title="A sesssion-fixture which can look at all collected tests"
             accesskey="P">previous</a> |</li>
        <li><a href="../contents.html">pytest-2.3.4.1</a> &raquo;</li>
          <li><a href="index.html" accesskey="U">Usages and Examples</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="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="#">Changing standard (Python) test discovery</a><ul>
<li><a class="reference internal" href="#changing-directory-recursion">Changing directory recursion</a></li>
<li><a class="reference internal" href="#changing-naming-conventions">Changing naming conventions</a></li>
<li><a class="reference internal" href="#interpreting-cmdline-arguments-as-python-packages">Interpreting cmdline arguments as Python packages</a></li>
<li><a class="reference internal" href="#finding-out-what-is-collected">Finding out what is collected</a></li>
<li><a class="reference internal" href="#customizing-test-collection-to-find-all-py-files">customizing test collection to find all .py files</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="special.html"
                        title="previous chapter">A sesssion-fixture which can look at all collected tests</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="nonpython.html"
                        title="next chapter">Working with non-python tests</a></p>
        </div>
      </div>

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="changing-standard-python-test-discovery">
<h1>Changing standard (Python) test discovery<a class="headerlink" href="#changing-standard-python-test-discovery" title="Permalink to this headline">¶</a></h1>
<div class="section" id="changing-directory-recursion">
<h2>Changing directory recursion<a class="headerlink" href="#changing-directory-recursion" title="Permalink to this headline">¶</a></h2>
<p>You can set the <a class="reference internal" href="../customize.html#confval-norecursedirs"><tt class="xref std std-confval docutils literal"><span class="pre">norecursedirs</span></tt></a> option in an ini-file, for example your <tt class="docutils literal"><span class="pre">setup.cfg</span></tt> in the project root directory:</p>
<div class="highlight-python"><pre># content of setup.cfg
[pytest]
norecursedirs = .svn _build tmp*</pre>
</div>
<p>This would tell py.test to not recurse into typical subversion or sphinx-build directories or into any <tt class="docutils literal"><span class="pre">tmp</span></tt> prefixed directory.</p>
</div>
<div class="section" id="changing-naming-conventions">
<span id="change-naming-conventions"></span><h2>Changing naming conventions<a class="headerlink" href="#changing-naming-conventions" title="Permalink to this headline">¶</a></h2>
<p>You can configure different naming conventions by setting
the <a class="reference internal" href="../customize.html#confval-python_files"><tt class="xref std std-confval docutils literal"><span class="pre">python_files</span></tt></a>, <a class="reference internal" href="../customize.html#confval-python_classes"><tt class="xref std std-confval docutils literal"><span class="pre">python_classes</span></tt></a> and
<a class="reference internal" href="../customize.html#confval-python_functions"><tt class="xref std std-confval docutils literal"><span class="pre">python_functions</span></tt></a> configuration options.  Example:</p>
<div class="highlight-python"><pre># content of setup.cfg
# can also be defined in in tox.ini or pytest.ini file
[pytest]
python_files=check_*.py
python_classes=Check
python_functions=check</pre>
</div>
<p>This would make py.test look for <tt class="docutils literal"><span class="pre">check_</span></tt> prefixes in
Python filenames, <tt class="docutils literal"><span class="pre">Check</span></tt> prefixes in classes and <tt class="docutils literal"><span class="pre">check</span></tt> prefixes
in functions and classes.  For example, if we have:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># content of check_myapp.py</span>
<span class="k">class</span> <span class="nc">CheckMyApp</span><span class="p">:</span>
    <span class="k">def</span> <span class="nf">check_simple</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">check_complex</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">pass</span>
</pre></div>
</div>
<p>then the test collection looks like this:</p>
<div class="highlight-python"><pre>$ py.test --collectonly
=========================== test session starts ============================
platform linux2 -- Python 2.7.3 -- pytest-2.3.5
collected 2 items
&lt;Module 'check_myapp.py'&gt;
  &lt;Class 'CheckMyApp'&gt;
    &lt;Instance '()'&gt;
      &lt;Function 'check_simple'&gt;
      &lt;Function 'check_complex'&gt;

=============================  in 0.01 seconds =============================</pre>
</div>
</div>
<div class="section" id="interpreting-cmdline-arguments-as-python-packages">
<h2>Interpreting cmdline arguments as Python packages<a class="headerlink" href="#interpreting-cmdline-arguments-as-python-packages" title="Permalink to this headline">¶</a></h2>
<p>You can use the <tt class="docutils literal"><span class="pre">--pyargs</span></tt> option to make py.test try
interpreting arguments as python package names, deriving
their file system path and then running the test. For
example if you have unittest2 installed you can type:</p>
<div class="highlight-python"><pre>py.test --pyargs unittest2.test.test_skipping -q</pre>
</div>
<p>which would run the respective test module.  Like with
other options, through an ini-file and the <a class="reference internal" href="../customize.html#confval-addopts"><tt class="xref std std-confval docutils literal"><span class="pre">addopts</span></tt></a> option you
can make this change more permanently:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># content of pytest.ini</span>
<span class="p">[</span><span class="n">pytest</span><span class="p">]</span>
<span class="n">addopts</span> <span class="o">=</span> <span class="o">--</span><span class="n">pyargs</span>
</pre></div>
</div>
<p>Now a simple invocation of <tt class="docutils literal"><span class="pre">py.test</span> <span class="pre">NAME</span></tt> will check
if NAME exists as an importable package/module and otherwise
treat it as a filesystem path.</p>
</div>
<div class="section" id="finding-out-what-is-collected">
<h2>Finding out what is collected<a class="headerlink" href="#finding-out-what-is-collected" title="Permalink to this headline">¶</a></h2>
<p>You can always peek at the collection tree without running tests like this:</p>
<div class="highlight-python"><pre>. $ py.test --collectonly pythoncollection.py
=========================== test session starts ============================
platform linux2 -- Python 2.7.3 -- pytest-2.3.5
collected 3 items
&lt;Module 'pythoncollection.py'&gt;
  &lt;Function 'test_function'&gt;
  &lt;Class 'TestClass'&gt;
    &lt;Instance '()'&gt;
      &lt;Function 'test_method'&gt;
      &lt;Function 'test_anothermethod'&gt;

=============================  in 0.01 seconds =============================</pre>
</div>
</div>
<div class="section" id="customizing-test-collection-to-find-all-py-files">
<h2>customizing test collection to find all .py files<a class="headerlink" href="#customizing-test-collection-to-find-all-py-files" title="Permalink to this headline">¶</a></h2>
<p>You can easily instruct py.test to discover tests from every python file:</p>
<div class="highlight-python"><pre># content of pytest.ini
[pytest]
python_files = *.py</pre>
</div>
<p>However, many projects will have a <tt class="docutils literal"><span class="pre">setup.py</span></tt> which they don&#8217;t want to be imported. Moreover, there may files only importable by a specific python version.
For such cases you can dynamically define files to be ignored by listing
them in a <tt class="docutils literal"><span class="pre">conftest.py</span></tt> file:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># content of conftest.py</span>
<span class="kn">import</span> <span class="nn">sys</span>

<span class="n">collect_ignore</span> <span class="o">=</span> <span class="p">[</span><span class="s">&quot;setup.py&quot;</span><span class="p">]</span>
<span class="k">if</span> <span class="n">sys</span><span class="o">.</span><span class="n">version_info</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">&gt;</span> <span class="mi">2</span><span class="p">:</span>
    <span class="n">collect_ignore</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s">&quot;pkg/module_py2.py&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>And then if you have a module file like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># content of pkg/module_py2.py</span>
<span class="k">def</span> <span class="nf">test_only_on_python2</span><span class="p">():</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="k">assert</span> <span class="mi">0</span>
    <span class="k">except</span> <span class="ne">Exception</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span>
        <span class="k">pass</span>
</pre></div>
</div>
<p>and a setup.py dummy file like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># content of setup.py</span>
<span class="mi">0</span><span class="o">/</span><span class="mi">0</span>  <span class="c"># will raise exeption if imported</span>
</pre></div>
</div>
<p>then a pytest run on python2 will find the one test when run with a python2
interpreters and will leave out the setup.py file:</p>
<div class="highlight-python"><pre>$ py.test --collectonly
=========================== test session starts ============================
platform linux2 -- Python 2.7.3 -- pytest-2.3.5
collected 1 items
&lt;Module 'pkg/module_py2.py'&gt;
  &lt;Function 'test_only_on_python2'&gt;

=============================  in 0.01 seconds =============================</pre>
</div>
<p>If you run with a Python3 interpreter the moduled added through the conftest.py file will not be considered for test collection.</p>
</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="nonpython.html" title="Working with non-python tests"
             >next</a></li>
        <li class="right" >
          <a href="special.html" title="A sesssion-fixture which can look at all collected tests"
             >previous</a> |</li>
        <li><a href="../contents.html">pytest-2.3.4.1</a> &raquo;</li>
          <li><a href="index.html" >Usages and Examples</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>