/usr/share/doc/python-betamax-doc/html/usage_patterns.html is in python-betamax-doc 0.8.0-1.
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 | <!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>Usage Patterns — Betamax 0.8.0 documentation</title>
<link rel="stylesheet" href="_static/alabaster.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '0.8.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="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="top" title="Betamax 0.8.0 documentation" href="index.html" />
<link rel="next" title="Integrating Betamax with Test Frameworks" href="integrations.html" />
<link rel="prev" title="Third-Party Packages" href="third_party_packages.html" />
<link rel="stylesheet" href="_static/custom.css" type="text/css" />
<meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />
</head>
<body role="document">
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="usage-patterns">
<h1>Usage Patterns<a class="headerlink" href="#usage-patterns" title="Permalink to this headline">¶</a></h1>
<p>Below are suggested patterns for using Betamax efficiently.</p>
<div class="section" id="configuring-betamax-in-py-test-s-conftest-py">
<h2>Configuring Betamax in py.test’s conftest.py<a class="headerlink" href="#configuring-betamax-in-py-test-s-conftest-py" title="Permalink to this headline">¶</a></h2>
<p>Betamax and github3.py (the project which instigated the creation of Betamax)
both utilize <a class="reference external" href="http://pytest.org/latest/">py.test</a> and its feature of configuring how the tests run with
<code class="docutils literal"><span class="pre">conftest.py</span></code> <a class="footnote-reference" href="#id2" id="id1">[1]</a>. One pattern that I have found useful is to include this
in your <code class="docutils literal"><span class="pre">conftest.py</span></code> file:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">betamax</span>
<span class="k">with</span> <span class="n">betamax</span><span class="o">.</span><span class="n">Betamax</span><span class="o">.</span><span class="n">configure</span><span class="p">()</span> <span class="k">as</span> <span class="n">config</span><span class="p">:</span>
<span class="n">config</span><span class="o">.</span><span class="n">cassette_library_dir</span> <span class="o">=</span> <span class="s1">'tests/cassettes/'</span>
</pre></div>
</div>
<p>This configures your cassette directory for all of your tests. If you do not
check your cassettes into your version control system, then you can also add:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">os</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="s1">'tests/cassettes'</span><span class="p">):</span>
<span class="n">os</span><span class="o">.</span><span class="n">makedirs</span><span class="p">(</span><span class="s1">'tests/cassettes'</span><span class="p">)</span>
</pre></div>
</div>
<div class="section" id="an-example-from-github3-py">
<h3>An Example from github3.py<a class="headerlink" href="#an-example-from-github3-py" title="Permalink to this headline">¶</a></h3>
<p>You can configure other aspects of Betamax via the <code class="docutils literal"><span class="pre">conftest.py</span></code> file. For
example, in github3.py, I do the following:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">os</span>
<span class="n">record_mode</span> <span class="o">=</span> <span class="s1">'none'</span> <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'TRAVIS_GH3'</span><span class="p">)</span> <span class="k">else</span> <span class="s1">'once'</span>
<span class="k">with</span> <span class="n">betamax</span><span class="o">.</span><span class="n">Betamax</span><span class="o">.</span><span class="n">configure</span><span class="p">()</span> <span class="k">as</span> <span class="n">config</span><span class="p">:</span>
<span class="n">config</span><span class="o">.</span><span class="n">cassette_library_dir</span> <span class="o">=</span> <span class="s1">'tests/cassettes/'</span>
<span class="n">config</span><span class="o">.</span><span class="n">default_cassette_options</span><span class="p">[</span><span class="s1">'record_mode'</span><span class="p">]</span> <span class="o">=</span> <span class="n">record_mode</span>
<span class="n">config</span><span class="o">.</span><span class="n">define_cassette_placeholder</span><span class="p">(</span>
<span class="s1">'<AUTH_TOKEN>'</span><span class="p">,</span>
<span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'GH_AUTH'</span><span class="p">,</span> <span class="s1">'x'</span> <span class="o">*</span> <span class="mi">20</span><span class="p">)</span>
<span class="p">)</span>
</pre></div>
</div>
<p>In essence, if the tests are being run on <a class="reference external" href="https://travis-ci.org/">Travis CI</a>, then we want to make
sure to not try to record new cassettes or interactions. We also, want to
ensure we’re authenticated when possible but that we do not leave our
placeholder in the cassettes when they’re replayed.</p>
</div>
</div>
<div class="section" id="using-human-readble-json-cassettes">
<h2>Using Human Readble JSON Cassettes<a class="headerlink" href="#using-human-readble-json-cassettes" title="Permalink to this headline">¶</a></h2>
<p>Using the <code class="docutils literal"><span class="pre">PrettyJSONSerializer</span></code> provided by the <code class="docutils literal"><span class="pre">betamax_serializers</span></code>
package provides human readable JSON cassettes. Cassettes output in this way
make it easy to compare modifications to cassettes to ensure only expected
changes are introduced.</p>
<p>While you can use the <code class="docutils literal"><span class="pre">serialize_with</span></code> option when creating each individual
cassette, it is simpler to provide this setting globally. The following example
demonstrates how to configure Betamax to use the <code class="docutils literal"><span class="pre">PrettyJSONSerializer</span></code> for
all newly created cassettes:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">betamax_serializers</span> <span class="kn">import</span> <span class="n">pretty_json</span>
<span class="n">betamax</span><span class="o">.</span><span class="n">Betamax</span><span class="o">.</span><span class="n">register_serializer</span><span class="p">(</span><span class="n">pretty_json</span><span class="o">.</span><span class="n">PrettyJSONSerializer</span><span class="p">)</span>
<span class="c1"># ...</span>
<span class="n">config</span><span class="o">.</span><span class="n">default_cassette_options</span><span class="p">[</span><span class="s1">'serialize_with'</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'prettyjson'</span>
</pre></div>
</div>
<div class="section" id="updating-existing-betamax-cassettes-to-be-human-readable">
<h3>Updating Existing Betamax Cassettes to be Human Readable<a class="headerlink" href="#updating-existing-betamax-cassettes-to-be-human-readable" title="Permalink to this headline">¶</a></h3>
<p>If you already have a library of cassettes when applying the previous
configuration update, then you will probably want to also update all your
existing cassettes into the new human readable format. The following script
will help you transform your existing cassettes:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">glob</span>
<span class="kn">import</span> <span class="nn">json</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">cassette_dir</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
<span class="n">cassettes</span> <span class="o">=</span> <span class="n">glob</span><span class="o">.</span><span class="n">glob</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">cassette_dir</span><span class="p">,</span> <span class="s1">'*.json'</span><span class="p">))</span>
<span class="k">except</span><span class="p">:</span>
<span class="k">print</span><span class="p">(</span><span class="s1">'Usage: {0} CASSETTE_DIRECTORY'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">0</span><span class="p">]))</span>
<span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="k">for</span> <span class="n">cassette_path</span> <span class="ow">in</span> <span class="n">cassettes</span><span class="p">:</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">cassette_path</span><span class="p">,</span> <span class="s1">'r'</span><span class="p">)</span> <span class="k">as</span> <span class="n">fp</span><span class="p">:</span>
<span class="n">data</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">fp</span><span class="p">)</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">cassette_path</span><span class="p">,</span> <span class="s1">'w'</span><span class="p">)</span> <span class="k">as</span> <span class="n">fp</span><span class="p">:</span>
<span class="n">json</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">fp</span><span class="p">,</span> <span class="n">sort_keys</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">indent</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span>
<span class="n">separators</span><span class="o">=</span><span class="p">(</span><span class="s1">','</span><span class="p">,</span> <span class="s1">': '</span><span class="p">))</span>
<span class="k">print</span><span class="p">(</span><span class="s1">'Updated {0} cassette{1}.'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
<span class="nb">len</span><span class="p">(</span><span class="n">cassettes</span><span class="p">),</span> <span class="s1">''</span> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">cassettes</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span> <span class="k">else</span> <span class="s1">'s'</span><span class="p">))</span>
</pre></div>
</div>
<p>Copy and save the above script as <code class="docutils literal"><span class="pre">fix_cassettes.py</span></code> and then run it like:</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span>python fix_cassettes.py PATH_TO_CASSETTE_DIRECTORY
</pre></div>
</div>
<p>If you’re not already using a version control system (e.g., git, svn) then it
is recommended you make a backup of your cassettes first in the event something
goes wrong.</p>
<table class="docutils footnote" frame="void" id="id2" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id1">[1]</a></td><td><a class="reference external" href="http://pytest.org/latest/plugins.html">http://pytest.org/latest/plugins.html</a></td></tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h3><a href="index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Usage Patterns</a><ul>
<li><a class="reference internal" href="#configuring-betamax-in-py-test-s-conftest-py">Configuring Betamax in py.test’s conftest.py</a><ul>
<li><a class="reference internal" href="#an-example-from-github3-py">An Example from github3.py</a></li>
</ul>
</li>
<li><a class="reference internal" href="#using-human-readble-json-cassettes">Using Human Readble JSON Cassettes</a><ul>
<li><a class="reference internal" href="#updating-existing-betamax-cassettes-to-be-human-readable">Updating Existing Betamax Cassettes to be Human Readable</a></li>
</ul>
</li>
</ul>
</li>
</ul>
<div class="relations">
<h3>Related Topics</h3>
<ul>
<li><a href="index.html">Documentation overview</a><ul>
<li>Previous: <a href="third_party_packages.html" title="previous chapter">Third-Party Packages</a></li>
<li>Next: <a href="integrations.html" title="next chapter">Integrating Betamax with Test Frameworks</a></li>
</ul></li>
</ul>
</div>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/usage_patterns.txt"
rel="nofollow">Show Source</a></li>
</ul>
</div>
<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">
©2013-2016 - Ian Cordasco.
|
Powered by <a href="http://sphinx-doc.org/">Sphinx 1.4.8</a>
& <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.8</a>
|
<a href="_sources/usage_patterns.txt"
rel="nofollow">Page source</a>
</div>
</body>
</html>
|