Current File : //usr/share/doc/pytest-2.7.0/html/en/example/pythoncollection.html |
<!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/flasky.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.7.0',
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="shortcut icon" href="../_static/pytest1favi.ico"/>
<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 session-fixture which can look at all collected tests" href="special.html" />
<meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9">
</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 session-fixture which can look at all collected tests"
accesskey="P">previous</a> |</li>
<li><a href="../contents.html">pytest-2.7.0</a> »</li>
<li><a href="index.html" accesskey="U">Usages and Examples</a> »</li>
</ul>
</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 <tt class="docutils literal"><span class="pre">pytest</span></tt> 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 <tt class="docutils literal"><span class="pre">pytest</span></tt> look for tests in files that match the <tt class="docutils literal"><span class="pre">check_*</span>
<span class="pre">.py</span></tt> glob-pattern, <tt class="docutils literal"><span class="pre">Check</span></tt> prefixes in classes, and functions and methods
that match <tt class="docutils literal"><span class="pre">*_check</span></tt>. 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">simple_check</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">complex_check</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 --collect-only
=========================== test session starts ============================
platform linux -- Python 3.4.0 -- py-1.4.26 -- pytest-2.7.0
rootdir: /tmp/doc-exec-170, inifile: setup.cfg
collected 2 items
<Module 'check_myapp.py'>
<Class 'CheckMyApp'>
<Instance '()'>
<Function 'simple_check'>
<Function 'complex_check'>
============================= in 0.01 seconds =============================</pre>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">the <tt class="docutils literal"><span class="pre">python_functions</span></tt> and <tt class="docutils literal"><span class="pre">python_classes</span></tt> options has no effect
for <tt class="docutils literal"><span class="pre">unittest.TestCase</span></tt> test discovery because pytest delegates
detection of test case methods to unittest code.</p>
</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 <tt class="docutils literal"><span class="pre">pytest</span></tt> 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 --collect-only pythoncollection.py
=========================== test session starts ============================
platform linux -- Python 3.4.0 -- py-1.4.26 -- pytest-2.7.0
rootdir: /home/hpk/p/pytest/doc/en, inifile: pytest.ini
collected 3 items
<Module 'example/pythoncollection.py'>
<Function 'test_function'>
<Class 'TestClass'>
<Instance '()'>
<Function 'test_method'>
<Function 'test_anothermethod'>
============================= 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 <tt class="docutils literal"><span class="pre">pytest</span></tt> 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’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">"setup.py"</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">></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">"pkg/module_py2.py"</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 --collect-only
=========================== test session starts ============================
platform linux -- Python 3.4.0 -- py-1.4.26 -- pytest-2.7.0
rootdir: /tmp/doc-exec-170, inifile: pytest.ini
collected 0 items
============================= in 0.00 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="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<p class="logo"><a href="../contents.html">
<img class="logo" src="../_static/pytest1.png" alt="Logo"/>
</a></p><h3><a href="../contents.html">Table Of Contents</a></h3>
<ul>
<li><a href="../index.html">Home</a></li>
<li><a href="../contents.html">Contents</a></li>
<li><a href="../getting-started.html">Install</a></li>
<li><a href="index.html">Examples</a></li>
<li><a href="../customize.html">Customize</a></li>
<li><a href="../contact.html">Contact</a></li>
<li><a href="../talks.html">Talks/Posts</a></li>
<li><a href="../changelog.html">Changelog</a></li>
</ul>
<hr>
<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>
<h3>Related Topics</h3>
<ul>
<li><a href="../contents.html">Documentation overview</a><ul>
<li><a href="index.html">Usages and Examples</a><ul>
<li>Previous: <a href="special.html" title="previous chapter">A session-fixture which can look at all collected tests</a></li>
<li>Next: <a href="nonpython.html" title="next chapter">Working with non-python tests</a></li>
</ul></li>
</ul></li>
</ul><h3>Useful Links</h3>
<ul>
<li><a href="../index.html">The pytest Website</a></li>
<li><a href="../contributing.html">Contribution Guide</a></li>
<li><a href="https://pypi.python.org/pypi/pytest">pytest @ PyPI</a></li>
<li><a href="https://bitbucket.org/pytest-dev/pytest/">pytest @ Bitbucket</a></li>
<li><a href="http://pytest.org/latest/plugins_index/index.html">3rd party plugins</a></li>
<li><a href="https://bitbucket.org/pytest-dev/pytest/issues?status=new&status=open">Issue Tracker</a></li>
<li><a href="http://pytest.org/latest/pytest.pdf">PDF Documentation</a>
</ul>
<div id="searchbox" style="display: none">
<h3>Quick search</h3>
<form class="search" action="../search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer">
© Copyright 2014, holger krekel.
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a>.
</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>
</body>
</html>