/usr/share/doc/python-pytest/html/getting-started.html is in python-pytest-doc 3.3.2-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 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 | <!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>Installation and Getting Started — pytest documentation</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: '3.3',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true,
SOURCELINK_SUFFIX: '.txt'
};
</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="search" title="Search" href="search.html" />
<link rel="next" title="Usage and Invocations" href="usage.html" />
<link rel="prev" title="Full pytest documentation" href="contents.html" />
<meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9">
</head>
<body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="usage.html" title="Usage and Invocations"
accesskey="N">next</a></li>
<li class="right" >
<a href="contents.html" title="Full pytest documentation"
accesskey="P">previous</a> |</li>
<li class="nav-item nav-item-0"><a href="contents.html">pytest-3.3</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="installation-and-getting-started">
<h1>Installation and Getting Started<a class="headerlink" href="#installation-and-getting-started" title="Permalink to this headline">¶</a></h1>
<p><strong>Pythons</strong>: Python 2.7, 3.4, 3.5, 3.6, Jython, PyPy-2.3</p>
<p><strong>Platforms</strong>: Unix/Posix and Windows</p>
<p><strong>PyPI package name</strong>: <a class="reference external" href="http://pypi.python.org/pypi/pytest">pytest</a></p>
<p><strong>dependencies</strong>: <a class="reference external" href="http://pypi.python.org/pypi/py">py</a>,
<a class="reference external" href="http://pypi.python.org/pypi/colorama">colorama (Windows)</a>,</p>
<p><strong>documentation as PDF</strong>: <a class="reference external" href="https://media.readthedocs.org/pdf/pytest/latest/pytest.pdf">download latest</a></p>
<div class="section" id="installation">
<span id="getstarted"></span><span id="id1"></span><h2>Installation<a class="headerlink" href="#installation" title="Permalink to this headline">¶</a></h2>
<p>Installation:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">pip</span> <span class="n">install</span> <span class="o">-</span><span class="n">U</span> <span class="n">pytest</span>
</pre></div>
</div>
<p>To check your installation has installed the correct version:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span>$ pytest --version
This is pytest version 3.x.y, imported from $PYTHON_PREFIX/lib/python3.5/site-packages/pytest.py
</pre></div>
</div>
</div>
<div class="section" id="our-first-test-run">
<span id="simpletest"></span><h2>Our first test run<a class="headerlink" href="#our-first-test-run" title="Permalink to this headline">¶</a></h2>
<p>Let's create a first test file with a simple test function:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="c1"># content of test_sample.py</span>
<span class="k">def</span> <span class="nf">func</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
<span class="k">return</span> <span class="n">x</span> <span class="o">+</span> <span class="mi">1</span>
<span class="k">def</span> <span class="nf">test_answer</span><span class="p">():</span>
<span class="k">assert</span> <span class="n">func</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span> <span class="o">==</span> <span class="mi">5</span>
</pre></div>
</div>
<p>That's it. You can execute the test function now:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span>$ pytest
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y
rootdir: $REGENDOC_TMPDIR, inifile:
collected 1 item
test_sample.py F [100%]
================================= FAILURES =================================
_______________________________ test_answer ________________________________
def test_answer():
> assert func(3) == 5
E assert 4 == 5
E + where 4 = func(3)
test_sample.py:5: AssertionError
========================= 1 failed in 0.12 seconds =========================
</pre></div>
</div>
<p>We got a failure report because our little <code class="docutils literal"><span class="pre">func(3)</span></code> call did not return <code class="docutils literal"><span class="pre">5</span></code>.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">You can simply use the <code class="docutils literal"><span class="pre">assert</span></code> statement for asserting test
expectations. pytest's <a class="reference internal" href="assert.html#assert-introspection"><span class="std std-ref">Advanced assertion introspection</span></a> will intelligently
report intermediate values of the assert expression freeing
you from the need to learn the many names of <a class="reference external" href="http://docs.python.org/library/unittest.html#test-cases">JUnit legacy methods</a>.</p>
</div>
</div>
<div class="section" id="running-multiple-tests">
<h2>Running multiple tests<a class="headerlink" href="#running-multiple-tests" title="Permalink to this headline">¶</a></h2>
<p><code class="docutils literal"><span class="pre">pytest</span></code> will run all files in the current directory and its subdirectories of the form test_*.py or *_test.py. More generally, it follows <a class="reference internal" href="goodpractices.html#test-discovery"><span class="std std-ref">standard test discovery rules</span></a>.</p>
</div>
<div class="section" id="asserting-that-a-certain-exception-is-raised">
<h2>Asserting that a certain exception is raised<a class="headerlink" href="#asserting-that-a-certain-exception-is-raised" title="Permalink to this headline">¶</a></h2>
<p>If you want to assert that some code raises an exception you can
use the <code class="docutils literal"><span class="pre">raises</span></code> helper:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="c1"># content of test_sysexit.py</span>
<span class="kn">import</span> <span class="nn">pytest</span>
<span class="k">def</span> <span class="nf">f</span><span class="p">():</span>
<span class="k">raise</span> <span class="ne">SystemExit</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">test_mytest</span><span class="p">():</span>
<span class="k">with</span> <span class="n">pytest</span><span class="o">.</span><span class="n">raises</span><span class="p">(</span><span class="ne">SystemExit</span><span class="p">):</span>
<span class="n">f</span><span class="p">()</span>
</pre></div>
</div>
<p>Running it with, this time in "quiet" reporting mode:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span>$ pytest -q test_sysexit.py
. [100%]
1 passed in 0.12 seconds
</pre></div>
</div>
</div>
<div class="section" id="grouping-multiple-tests-in-a-class">
<h2>Grouping multiple tests in a class<a class="headerlink" href="#grouping-multiple-tests-in-a-class" title="Permalink to this headline">¶</a></h2>
<p>Once you start to have more than a few tests it often makes sense
to group tests logically, in classes and modules. Let's write a class
containing two tests:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="c1"># content of test_class.py</span>
<span class="k">class</span> <span class="nc">TestClass</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">test_one</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="n">x</span> <span class="o">=</span> <span class="s2">"this"</span>
<span class="k">assert</span> <span class="s1">'h'</span> <span class="ow">in</span> <span class="n">x</span>
<span class="k">def</span> <span class="nf">test_two</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="n">x</span> <span class="o">=</span> <span class="s2">"hello"</span>
<span class="k">assert</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="s1">'check'</span><span class="p">)</span>
</pre></div>
</div>
<p>The two tests are found because of the standard <a class="reference internal" href="goodpractices.html#test-discovery"><span class="std std-ref">Conventions for Python test discovery</span></a>.
There is no need to subclass anything. We can simply
run the module by passing its filename:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span>$ pytest -q test_class.py
.F [100%]
================================= FAILURES =================================
____________________________ TestClass.test_two ____________________________
self = <test_class.TestClass object at 0xdeadbeef>
def test_two(self):
x = "hello"
> assert hasattr(x, 'check')
E AssertionError: assert False
E + where False = hasattr('hello', 'check')
test_class.py:8: AssertionError
1 failed, 1 passed in 0.12 seconds
</pre></div>
</div>
<p>The first test passed, the second failed. Again we can easily see
the intermediate values used in the assertion, helping us to
understand the reason for the failure.</p>
</div>
<div class="section" id="going-functional-requesting-a-unique-temporary-directory">
<h2>Going functional: requesting a unique temporary directory<a class="headerlink" href="#going-functional-requesting-a-unique-temporary-directory" title="Permalink to this headline">¶</a></h2>
<p>For functional tests one often needs to create some files
and pass them to application objects. pytest provides
<a class="reference internal" href="builtin.html#builtinfixtures"><span class="std std-ref">Builtin fixtures/function arguments</span></a> which allow to request arbitrary
resources, for example a unique temporary directory:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="c1"># content of test_tmpdir.py</span>
<span class="k">def</span> <span class="nf">test_needsfiles</span><span class="p">(</span><span class="n">tmpdir</span><span class="p">):</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">tmpdir</span><span class="p">)</span>
<span class="k">assert</span> <span class="mi">0</span>
</pre></div>
</div>
<p>We list the name <code class="docutils literal"><span class="pre">tmpdir</span></code> in the test function signature and
<code class="docutils literal"><span class="pre">pytest</span></code> will lookup and call a fixture factory to create the resource
before performing the test function call. Let's just run it:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span>$ pytest -q test_tmpdir.py
F [100%]
================================= FAILURES =================================
_____________________________ test_needsfiles ______________________________
tmpdir = local('PYTEST_TMPDIR/test_needsfiles0')
def test_needsfiles(tmpdir):
print (tmpdir)
> assert 0
E assert 0
test_tmpdir.py:3: AssertionError
--------------------------- Captured stdout call ---------------------------
PYTEST_TMPDIR/test_needsfiles0
1 failed in 0.12 seconds
</pre></div>
</div>
<p>Before the test runs, a unique-per-test-invocation temporary directory
was created. More info at <a class="reference internal" href="tmpdir.html#tmpdir-handling"><span class="std std-ref">Temporary directories and files</span></a>.</p>
<p>You can find out what kind of builtin <a class="reference internal" href="fixture.html#fixtures"><span class="std std-ref">pytest fixtures: explicit, modular, scalable</span></a> exist by typing:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">pytest</span> <span class="o">--</span><span class="n">fixtures</span> <span class="c1"># shows builtin and custom fixtures</span>
</pre></div>
</div>
</div>
<div class="section" id="where-to-go-next">
<h2>Where to go next<a class="headerlink" href="#where-to-go-next" title="Permalink to this headline">¶</a></h2>
<p>Here are a few suggestions where to go next:</p>
<ul class="simple">
<li><a class="reference internal" href="usage.html#cmdline"><span class="std std-ref">Calling pytest through python -m pytest</span></a> for command line invocation examples</li>
<li><a class="reference internal" href="goodpractices.html#goodpractices"><span class="std std-ref">good practices</span></a> for virtualenv, test layout</li>
<li><a class="reference internal" href="existingtestsuite.html#existingtestsuite"><span class="std std-ref">Using pytest with an existing test suite</span></a> for working with pre-existing tests</li>
<li><a class="reference internal" href="fixture.html#fixtures"><span class="std std-ref">pytest fixtures: explicit, modular, scalable</span></a> for providing a functional baseline to your tests</li>
<li><a class="reference internal" href="writing_plugins.html#plugins"><span class="std std-ref">Writing plugins</span></a> managing and writing plugins</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<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="#">Install</a></li>
<li><a href="example/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>
<li><a href="backwards-compatibility.html">Backwards Compatibility</a></li>
<li><a href="license.html">License</a></li>
</ul>
<hr>
<ul>
<li><a class="reference internal" href="#">Installation and Getting Started</a><ul>
<li><a class="reference internal" href="#installation">Installation</a></li>
<li><a class="reference internal" href="#our-first-test-run">Our first test run</a></li>
<li><a class="reference internal" href="#running-multiple-tests">Running multiple tests</a></li>
<li><a class="reference internal" href="#asserting-that-a-certain-exception-is-raised">Asserting that a certain exception is raised</a></li>
<li><a class="reference internal" href="#grouping-multiple-tests-in-a-class">Grouping multiple tests in a class</a></li>
<li><a class="reference internal" href="#going-functional-requesting-a-unique-temporary-directory">Going functional: requesting a unique temporary directory</a></li>
<li><a class="reference internal" href="#where-to-go-next">Where to go next</a></li>
</ul>
</li>
</ul>
<h3>Related Topics</h3>
<ul>
<li><a href="contents.html">Documentation overview</a><ul>
<li>Previous: <a href="contents.html" title="previous chapter">Full pytest documentation</a></li>
<li>Next: <a href="usage.html" title="next chapter">Usage and Invocations</a></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://github.com/pytest-dev/pytest/">pytest @ GitHub</a></li>
<li><a href="http://plugincompat.herokuapp.com/">3rd party plugins</a></li>
<li><a href="https://github.com/pytest-dev/pytest/issues">Issue Tracker</a></li>
<li><a href="https://media.readthedocs.org/pdf/pytest/latest/pytest.pdf">PDF Documentation</a>
</ul>
<div id="searchbox" style="display: none" role="search">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<div><input type="text" name="q" /></div>
<div><input type="submit" value="Go" /></div>
<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>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer">
© Copyright 2018, holger krekel and pytest-dev team.
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a>.
</div>
</body>
</html>
|