/usr/share/doc/python-pytest-doc/html/example/pythoncollection.html is in python-pytest-doc 2.6.3-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 | <!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.6.2',
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.6.2</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"><div class="highlight"><pre># content of setup.cfg
[pytest]
norecursedirs = .svn _build tmp*
</pre></div>
</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"><div class="highlight"><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>
</div>
<p>This would make <tt class="docutils literal"><span class="pre">pytest</span></tt> 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"><div class="highlight"><pre>$ py.test --collect-only
=========================== test session starts ============================
platform linux -- Python 3.4.0 -- py-1.4.25 -- pytest-2.6.3
collected 2 items
<Module 'check_myapp.py'>
<Class 'CheckMyApp'>
<Instance '()'>
<Function 'check_simple'>
<Function 'check_complex'>
============================= in 0.01 seconds =============================
</pre></div>
</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> 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"><div class="highlight"><pre>py.test --pyargs unittest2.test.test_skipping -q
</pre></div>
</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"><div class="highlight"><pre>. $ py.test --collect-only pythoncollection.py
=========================== test session starts ============================
platform linux -- Python 3.4.0 -- py-1.4.25 -- pytest-2.6.3
collected 3 items
<Module 'pythoncollection.py'>
<Function 'test_function'>
<Class 'TestClass'>
<Instance '()'>
<Function 'test_method'>
<Function 'test_anothermethod'>
============================= in 0.01 seconds =============================
</pre></div>
</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"><div class="highlight"><pre># content of pytest.ini
[pytest]
python_files = *.py
</pre></div>
</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"><div class="highlight"><pre>$ py.test --collect-only
=========================== test session starts ============================
platform linux -- Python 3.4.0 -- py-1.4.25 -- pytest-2.6.3
collected 0 items
============================= in 0.01 seconds =============================
</pre></div>
</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/hpk42/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/hpk42/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>
</body>
</html>
|