formations/python/python3/fr/_build/html/testsunitaires.html

208 lines
13 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!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" lang="fr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Tests unitaires et pile dappels &#8212; Documentation Formation Python 1</title>
<link rel="stylesheet" href="_static/classic.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '1',
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>
<script type="text/javascript" src="_static/translations.js"></script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Recherche" href="search.html" />
<link rel="next" title="Sphinx et docutils" href="docutils.html" />
<link rel="prev" title="Structures de contrôle et fonctions" href="structures.html" />
</head>
<body>
<div style="background-color: white; text-align: left; padding: 10px 10px 15px 15px">
<table><tr><td>
<img src="_static/sphinx.png" alt="logo" />
</td><td>
<h1>&nbsp; &nbsp; Programmation python</h1>
</td></tr></table>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="Index général"
accesskey="I">index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Index des modules Python"
>modules</a> |</li>
<li class="right" >
<a href="docutils.html" title="Sphinx et docutils"
accesskey="N">suivant</a> |</li>
<li class="right" >
<a href="structures.html" title="Structures de contrôle et fonctions"
accesskey="P">précédent</a> |</li>
<a href="index.html">Programmation python </a> &raquo;
</ul>
</div>
<p>&nbsp;</p>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="tests-unitaires-et-pile-d-appels">
<h1>Tests unitaires et pile dappels<a class="headerlink" href="#tests-unitaires-et-pile-d-appels" title="Lien permanent vers ce titre"></a></h1>
<p>Les tests automatiques sont un complément à la déclaration des types.</p>
<p>que tester, quoi tester ?</p>
<ul class="simple">
<li>que les composants interagissent bien entre eux</li>
<li>que les unités (fonctions, objets…) réagissent bien à une entrée spécifique</li>
<li>que le code se comporte de la manière attendue dans des environnements différents
(systèmes dexploitation, etc…)</li>
</ul>
<p>les types de tests :</p>
<ul class="simple">
<li>tests unitaires</li>
<li>tests fonctionnels</li>
<li>tests dacceptation</li>
<li>tests dintégration</li>
</ul>
<p>Quel outil utiliser ?</p>
<ul class="simple">
<li>la <a class="reference internal" href="stdlib.html"><span class="doc">La librairie standard</span></a> propose le module <a class="reference internal" href="stdlib.html#module-unittest" title="unittest: module de tests unitaires"><code class="xref py py-mod docutils literal"><span class="pre">unittest</span></code></a> et unittest2.</li>
</ul>
<span class="target" id="module-py.test"></span><ul class="simple">
<li><a class="reference internal" href="#module-py.test" title="py.test: outil de test unitaires de la pylib"><code class="xref py py-mod docutils literal"><span class="pre">py.test</span></code></a> est <strong>le plus simple</strong> (plus simple que unittest)</li>
</ul>
<p><a class="reference external" href="http://pytest.org/latest/">http://pytest.org/latest/</a></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">test_simple</span><span class="p">():</span>
<span class="s2">&quot;test si ma_fontion renvoie bien 3&quot;</span>
<span class="k">assert</span> <span class="n">ma_fontion</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="o">==</span> <span class="mi">3</span>
</pre></div>
</div>
<ul class="simple">
<li><cite>py.test</cite> utilise <cite>assert</cite> et <cite>raises</cite></li>
<li><cite>unittest</cite> necessite de faire sytématiquement une classe, puis <cite>assertEqual()</cite></li>
<li><a class="reference internal" href="stdlib.html#module-doctest" title="doctest: module de tests unitaires basé sur les docstrings"><code class="xref py py-mod docutils literal"><span class="pre">doctest</span></code></a> est plus simple mais charge trop les docstrings</li>
</ul>
<div class="admonition-todo admonition" id="index-0">
<p class="first admonition-title">À faire</p>
<p class="last">écrire un test unitaire avec <cite>py.test</cite> pour la fonction suivante</p>
</div>
<ul class="simple">
<li>installer le paquet <cite>python-pytest</cite></li>
<li>écrire un fichier avec une fonction dedans</li>
</ul>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">double</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">2</span>
</pre></div>
</div>
<ul class="simple">
<li>écrire un fichier commençant par <cite>test_</cite> qui teste les fonctions du fichier</li>
</ul>
<div class="section" id="options-utiles-dans-py-test">
<h2>options utiles dans <cite>py.test</cite><a class="headerlink" href="#options-utiles-dans-py-test" title="Lien permanent vers ce titre"></a></h2>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">py</span><span class="o">.</span><span class="n">test</span> <span class="o">--</span><span class="n">tb</span><span class="o">=</span><span class="n">no</span>
<span class="n">py</span><span class="o">.</span><span class="n">test</span> <span class="o">--</span><span class="n">tb</span><span class="o">=</span><span class="n">short</span>
<span class="n">py</span><span class="o">.</span><span class="n">test</span> <span class="o">-</span><span class="n">x</span> <span class="p">(</span><span class="n">dernière</span> <span class="n">erreur</span><span class="p">)</span>
<span class="n">py</span><span class="o">.</span><span class="n">test</span> <span class="o">-</span><span class="n">s</span> <span class="p">(</span><span class="n">c</span><span class="s1">&#39;est comme nocapture en fait)</span>
<span class="n">py</span><span class="o">.</span><span class="n">test</span> <span class="o">-</span><span class="n">x</span> <span class="o">--</span><span class="n">nocapture</span> <span class="o">--</span><span class="n">pdb</span>
<span class="n">py</span><span class="o">.</span><span class="n">test</span> <span class="o">-</span><span class="n">xl</span> <span class="o">--</span><span class="n">pdb</span>
<span class="n">py</span><span class="o">.</span><span class="n">test</span> <span class="o">-</span><span class="n">k</span> <span class="n">test_simple</span>
</pre></div>
</div>
<p>utiliser le module <a class="reference internal" href="#module-pdb" title="pdb: debugger de la lib standard"><code class="xref py py-mod docutils literal"><span class="pre">pdb</span></code></a></p>
<span class="target" id="module-pdb"></span><div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">pdb</span>
<span class="n">pdb</span><span class="o">.</span><span class="n">set_trace</span><span class="p">()</span>
</pre></div>
</div>
<p>depuis py.test :</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">pytest</span>
<span class="k">def</span> <span class="nf">test_function</span><span class="p">():</span>
<span class="o">...</span>
<span class="n">pytest</span><span class="o">.</span><span class="n">set_trace</span><span class="p">()</span> <span class="c1"># invoke PDB debugger and tracing</span>
</pre></div>
</div>
<p>Remarquons que <a class="reference internal" href="#module-pdb" title="pdb: debugger de la lib standard"><code class="xref py py-mod docutils literal"><span class="pre">pdb</span></code></a> utilise le module <a class="reference internal" href="prompt.html#module-cmd" title="cmd: interpréteur ligne de commande"><code class="xref py py-mod docutils literal"><span class="pre">cmd</span></code></a>, voir <a class="reference internal" href="prompt.html#cmdlabel"><span class="std std-ref">le module cmd et les interpréteurs</span></a> quon a déjà vu précedemment</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="p">(</span><span class="n">Pdb</span><span class="p">)</span> <span class="n">h</span>
<span class="n">Documented</span> <span class="n">commands</span> <span class="p">(</span><span class="nb">type</span> <span class="n">help</span> <span class="o">&lt;</span><span class="n">topic</span><span class="o">&gt;</span><span class="p">):</span>
<span class="o">========================================</span>
<span class="n">EOF</span> <span class="n">bt</span> <span class="n">cont</span> <span class="n">enable</span> <span class="n">jump</span> <span class="n">pp</span> <span class="n">run</span> <span class="n">unt</span>
<span class="n">a</span> <span class="n">c</span> <span class="k">continue</span> <span class="n">exit</span> <span class="n">l</span> <span class="n">q</span> <span class="n">s</span> <span class="n">until</span>
<span class="n">alias</span> <span class="n">cl</span> <span class="n">d</span> <span class="n">h</span> <span class="nb">list</span> <span class="n">quit</span> <span class="n">step</span> <span class="n">up</span>
<span class="n">args</span> <span class="n">clear</span> <span class="n">debug</span> <span class="n">help</span> <span class="n">n</span> <span class="n">r</span> <span class="n">tbreak</span> <span class="n">w</span>
<span class="n">b</span> <span class="n">commands</span> <span class="n">disable</span> <span class="n">ignore</span> <span class="nb">next</span> <span class="n">restart</span> <span class="n">u</span> <span class="n">whatis</span>
<span class="k">break</span> <span class="n">condition</span> <span class="n">down</span> <span class="n">j</span> <span class="n">p</span> <span class="k">return</span> <span class="n">unalias</span> <span class="n">where</span>
<span class="n">Miscellaneous</span> <span class="n">help</span> <span class="n">topics</span><span class="p">:</span>
<span class="o">==========================</span>
<span class="n">exec</span> <span class="n">pdb</span>
<span class="n">Undocumented</span> <span class="n">commands</span><span class="p">:</span>
<span class="o">======================</span>
<span class="n">retval</span> <span class="n">rv</span>
<span class="p">(</span><span class="n">Pdb</span><span class="p">)</span>
</pre></div>
</div>
<p>last but not least :</p>
<p>utiliser pylint ou pychecker</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">apt</span><span class="o">-</span><span class="n">get</span> <span class="n">install</span> <span class="n">pylint</span>
</pre></div>
</div>
</div>
</div>
</div>
</div>
</div>
<div style="background-color: white; text-align: left; padding: 10px 10px 15px 15px">
<a href="search.html"> Recherche</a> | &nbsp;
<!-- a href="genindex.html"> Genindex</a-->
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="Index général"
>index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Index des modules Python"
>modules</a> |</li>
<li class="right" >
<a href="docutils.html" title="Sphinx et docutils"
>suivant</a> |</li>
<li class="right" >
<a href="structures.html" title="Structures de contrôle et fonctions"
>précédent</a> |</li>
<a href="index.html">Programmation python </a> &raquo;
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2015, cadoles (www.cadoles.com).
Créé avec <a href="http://sphinx-doc.org/">Sphinx</a> 1.6.7.
</div>
</body>
</html>