/usr/share/doc/python-pytest/html/cache.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 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 | <!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>Cache: working with cross-testrun state — 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="unittest.TestCase Support" href="unittest.html" />
<link rel="prev" title="Parametrizing fixtures and test functions" href="parametrize.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="unittest.html" title="unittest.TestCase Support"
accesskey="N">next</a></li>
<li class="right" >
<a href="parametrize.html" title="Parametrizing fixtures and test functions"
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="cache-working-with-cross-testrun-state">
<span id="cache"></span><span id="cache-provider"></span><h1>Cache: working with cross-testrun state<a class="headerlink" href="#cache-working-with-cross-testrun-state" title="Permalink to this headline">¶</a></h1>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.8.</span></p>
</div>
<div class="section" id="usage">
<h2>Usage<a class="headerlink" href="#usage" title="Permalink to this headline">¶</a></h2>
<p>The plugin provides two command line options to rerun failures from the
last <code class="docutils literal"><span class="pre">pytest</span></code> invocation:</p>
<ul class="simple">
<li><code class="docutils literal"><span class="pre">--lf</span></code>, <code class="docutils literal"><span class="pre">--last-failed</span></code> - to only re-run the failures.</li>
<li><code class="docutils literal"><span class="pre">--ff</span></code>, <code class="docutils literal"><span class="pre">--failed-first</span></code> - to run the failures first and then the rest of
the tests.</li>
</ul>
<p>For cleanup (usually not needed), a <code class="docutils literal"><span class="pre">--cache-clear</span></code> option allows to remove
all cross-session cache contents ahead of a test run.</p>
<p>Other plugins may access the <a class="reference internal" href="#config-cache">config.cache</a> object to set/get
<strong>json encodable</strong> values between <code class="docutils literal"><span class="pre">pytest</span></code> invocations.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">This plugin is enabled by default, but can be disabled if needed: see
<a class="reference internal" href="plugins.html#cmdunregister"><span class="std std-ref">Deactivating / unregistering a plugin by name</span></a> (the internal name for this plugin is
<code class="docutils literal"><span class="pre">cacheprovider</span></code>).</p>
</div>
</div>
<div class="section" id="rerunning-only-failures-or-failures-first">
<h2>Rerunning only failures or failures first<a class="headerlink" href="#rerunning-only-failures-or-failures-first" title="Permalink to this headline">¶</a></h2>
<p>First, let's create 50 test invocation of which only 2 fail:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="c1"># content of test_50.py</span>
<span class="kn">import</span> <span class="nn">pytest</span>
<span class="nd">@pytest</span><span class="o">.</span><span class="n">mark</span><span class="o">.</span><span class="n">parametrize</span><span class="p">(</span><span class="s2">"i"</span><span class="p">,</span> <span class="nb">range</span><span class="p">(</span><span class="mi">50</span><span class="p">))</span>
<span class="k">def</span> <span class="nf">test_num</span><span class="p">(</span><span class="n">i</span><span class="p">):</span>
<span class="k">if</span> <span class="n">i</span> <span class="ow">in</span> <span class="p">(</span><span class="mi">17</span><span class="p">,</span> <span class="mi">25</span><span class="p">):</span>
<span class="n">pytest</span><span class="o">.</span><span class="n">fail</span><span class="p">(</span><span class="s2">"bad luck"</span><span class="p">)</span>
</pre></div>
</div>
<p>If you run this for the first time you will see two failures:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span>$ pytest -q
.................F.......F........................ [100%]
================================= FAILURES =================================
_______________________________ test_num[17] _______________________________
i = 17
@pytest.mark.parametrize("i", range(50))
def test_num(i):
if i in (17, 25):
> pytest.fail("bad luck")
E Failed: bad luck
test_50.py:6: Failed
_______________________________ test_num[25] _______________________________
i = 25
@pytest.mark.parametrize("i", range(50))
def test_num(i):
if i in (17, 25):
> pytest.fail("bad luck")
E Failed: bad luck
test_50.py:6: Failed
2 failed, 48 passed in 0.12 seconds
</pre></div>
</div>
<p>If you then run it with <code class="docutils literal"><span class="pre">--lf</span></code>:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span>$ pytest --lf
=========================== 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 50 items
run-last-failure: rerun previous 2 failures
test_50.py FF [100%]
================================= FAILURES =================================
_______________________________ test_num[17] _______________________________
i = 17
@pytest.mark.parametrize("i", range(50))
def test_num(i):
if i in (17, 25):
> pytest.fail("bad luck")
E Failed: bad luck
test_50.py:6: Failed
_______________________________ test_num[25] _______________________________
i = 25
@pytest.mark.parametrize("i", range(50))
def test_num(i):
if i in (17, 25):
> pytest.fail("bad luck")
E Failed: bad luck
test_50.py:6: Failed
=========================== 48 tests deselected ============================
================= 2 failed, 48 deselected in 0.12 seconds ==================
</pre></div>
</div>
<p>You have run only the two failing test from the last run, while 48 tests have
not been run ("deselected").</p>
<p>Now, if you run with the <code class="docutils literal"><span class="pre">--ff</span></code> option, all tests will be run but the first
previous failures will be executed first (as can be seen from the series
of <code class="docutils literal"><span class="pre">FF</span></code> and dots):</p>
<div class="highlight-default"><div class="highlight"><pre><span></span>$ pytest --ff
=========================== 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 50 items
run-last-failure: rerun previous 2 failures first
test_50.py FF................................................ [100%]
================================= FAILURES =================================
_______________________________ test_num[17] _______________________________
i = 17
@pytest.mark.parametrize("i", range(50))
def test_num(i):
if i in (17, 25):
> pytest.fail("bad luck")
E Failed: bad luck
test_50.py:6: Failed
_______________________________ test_num[25] _______________________________
i = 25
@pytest.mark.parametrize("i", range(50))
def test_num(i):
if i in (17, 25):
> pytest.fail("bad luck")
E Failed: bad luck
test_50.py:6: Failed
=================== 2 failed, 48 passed in 0.12 seconds ====================
</pre></div>
</div>
</div>
<div class="section" id="the-new-config-cache-object">
<span id="config-cache"></span><h2>The new config.cache object<a class="headerlink" href="#the-new-config-cache-object" title="Permalink to this headline">¶</a></h2>
<p>Plugins or conftest.py support code can get a cached value using the
pytest <code class="docutils literal"><span class="pre">config</span></code> object. Here is a basic example plugin which
implements a <a class="reference internal" href="fixture.html#fixture"><span class="std std-ref">pytest fixtures: explicit, modular, scalable</span></a> which re-uses previously created state
across pytest invocations:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="c1"># content of test_caching.py</span>
<span class="kn">import</span> <span class="nn">pytest</span>
<span class="kn">import</span> <span class="nn">time</span>
<span class="nd">@pytest</span><span class="o">.</span><span class="n">fixture</span>
<span class="k">def</span> <span class="nf">mydata</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
<span class="n">val</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">cache</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"example/value"</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
<span class="k">if</span> <span class="n">val</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">9</span><span class="o">*</span><span class="mf">0.6</span><span class="p">)</span> <span class="c1"># expensive computation :)</span>
<span class="n">val</span> <span class="o">=</span> <span class="mi">42</span>
<span class="n">request</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">cache</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">"example/value"</span><span class="p">,</span> <span class="n">val</span><span class="p">)</span>
<span class="k">return</span> <span class="n">val</span>
<span class="k">def</span> <span class="nf">test_function</span><span class="p">(</span><span class="n">mydata</span><span class="p">):</span>
<span class="k">assert</span> <span class="n">mydata</span> <span class="o">==</span> <span class="mi">23</span>
</pre></div>
</div>
<p>If you run this command once, it will take a while because
of the sleep:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span>$ pytest -q
F [100%]
================================= FAILURES =================================
______________________________ test_function _______________________________
mydata = 42
def test_function(mydata):
> assert mydata == 23
E assert 42 == 23
test_caching.py:14: AssertionError
1 failed in 0.12 seconds
</pre></div>
</div>
<p>If you run it a second time the value will be retrieved from
the cache and this will be quick:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span>$ pytest -q
F [100%]
================================= FAILURES =================================
______________________________ test_function _______________________________
mydata = 42
def test_function(mydata):
> assert mydata == 23
E assert 42 == 23
test_caching.py:14: AssertionError
1 failed in 0.12 seconds
</pre></div>
</div>
<p>See the <a class="reference internal" href="#cache-api">cache-api</a> for more details.</p>
</div>
<div class="section" id="inspecting-cache-content">
<h2>Inspecting Cache content<a class="headerlink" href="#inspecting-cache-content" title="Permalink to this headline">¶</a></h2>
<p>You can always peek at the content of the cache using the
<code class="docutils literal"><span class="pre">--cache-show</span></code> command line option:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span>$ py.test --cache-show
=========================== 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:
cachedir: $REGENDOC_TMPDIR/.cache
------------------------------- cache values -------------------------------
cache/lastfailed contains:
{'test_caching.py::test_function': True}
example/value contains:
42
======================= no tests ran in 0.12 seconds =======================
</pre></div>
</div>
</div>
<div class="section" id="clearing-cache-content">
<h2>Clearing Cache content<a class="headerlink" href="#clearing-cache-content" title="Permalink to this headline">¶</a></h2>
<p>You can instruct pytest to clear all cache files and values
by adding the <code class="docutils literal"><span class="pre">--cache-clear</span></code> option like this:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">pytest</span> <span class="o">--</span><span class="n">cache</span><span class="o">-</span><span class="n">clear</span>
</pre></div>
</div>
<p>This is recommended for invocations from Continuous Integration
servers where isolation and correctness is more important
than speed.</p>
</div>
<div class="section" id="config-cache-api">
<span id="cache-api"></span><h2>config.cache API<a class="headerlink" href="#config-cache-api" title="Permalink to this headline">¶</a></h2>
<p>The <code class="docutils literal"><span class="pre">config.cache</span></code> object allows other plugins,
including <code class="docutils literal"><span class="pre">conftest.py</span></code> files,
to safely and flexibly store and retrieve values across
test runs because the <code class="docutils literal"><span class="pre">config</span></code> object is available
in many places.</p>
<p>Under the hood, the cache plugin uses the simple
dumps/loads API of the json stdlib module</p>
<dl class="method">
<dt id="_pytest.cacheprovider.Cache.get">
<code class="descclassname">Cache.</code><code class="descname">get</code><span class="sig-paren">(</span><em>key</em>, <em>default</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/_pytest/cacheprovider.html#Cache.get"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#_pytest.cacheprovider.Cache.get" title="Permalink to this definition">¶</a></dt>
<dd><p>return cached value for the given key. If no value
was yet cached or the value cannot be read, the specified
default is returned.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>key</strong> -- must be a <code class="docutils literal"><span class="pre">/</span></code> separated value. Usually the first
name is the name of your plugin or your application.</li>
<li><strong>default</strong> -- must be provided in case of a cache-miss or
invalid cache values.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="method">
<dt id="_pytest.cacheprovider.Cache.set">
<code class="descclassname">Cache.</code><code class="descname">set</code><span class="sig-paren">(</span><em>key</em>, <em>value</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/_pytest/cacheprovider.html#Cache.set"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#_pytest.cacheprovider.Cache.set" title="Permalink to this definition">¶</a></dt>
<dd><p>save value for the given key.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>key</strong> -- must be a <code class="docutils literal"><span class="pre">/</span></code> separated value. Usually the first
name is the name of your plugin or your application.</li>
<li><strong>value</strong> -- must be of any combination of basic
python types, including nested types
like e. g. lists of dictionaries.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="method">
<dt id="_pytest.cacheprovider.Cache.makedir">
<code class="descclassname">Cache.</code><code class="descname">makedir</code><span class="sig-paren">(</span><em>name</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/_pytest/cacheprovider.html#Cache.makedir"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#_pytest.cacheprovider.Cache.makedir" title="Permalink to this definition">¶</a></dt>
<dd><p>return a directory path object with the given name. If the
directory does not yet exist, it will be created. You can use it
to manage files likes e. g. store/retrieve database
dumps across test sessions.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>name</strong> -- must be a string not containing a <code class="docutils literal"><span class="pre">/</span></code> separator.
Make sure the name contains your plugin or application
identifiers to prevent clashes with other cache users.</td>
</tr>
</tbody>
</table>
</dd></dl>
</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="getting-started.html">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="#">Cache: working with cross-testrun state</a><ul>
<li><a class="reference internal" href="#usage">Usage</a></li>
<li><a class="reference internal" href="#rerunning-only-failures-or-failures-first">Rerunning only failures or failures first</a></li>
<li><a class="reference internal" href="#the-new-config-cache-object">The new config.cache object</a></li>
<li><a class="reference internal" href="#inspecting-cache-content">Inspecting Cache content</a></li>
<li><a class="reference internal" href="#clearing-cache-content">Clearing Cache content</a></li>
<li><a class="reference internal" href="#config-cache-api">config.cache API</a></li>
</ul>
</li>
</ul>
<h3>Related Topics</h3>
<ul>
<li><a href="contents.html">Documentation overview</a><ul>
<li>Previous: <a href="parametrize.html" title="previous chapter">Parametrizing fixtures and test functions</a></li>
<li>Next: <a href="unittest.html" title="next chapter">unittest.TestCase Support</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>
|